0

Example: How do I compare one string out of a list against my desired string? Please help!

Dim myString As String                 

Dim myList As List(Of String)

Let's say myString Returns Bob and myList Returns (Bob, Mary, Sally, Joe)

I Need to do the following:

If(myString = myList) Then
      //Do some code
End If
3
  • does this help? stackoverflow.com/questions/26732563/… Commented Dec 31, 2020 at 14:32
  • 1
    A string is not equal to a list of string. However a list of strings can contain a particular string. Is that what you wanted to check for? Did you look at the methods available on the List class? Commented Dec 31, 2020 at 14:32
  • Consider using a HashSet instead Commented Dec 31, 2020 at 15:56

2 Answers 2

2

In order to compare a string out of a list of strings to your desired string, you must know where in the list the string is that you want to compare with.

Dim myString As String
Dim myList as List(Of String)

'this will compare your string to the first string in the list of strings
If myString = myList(0) Then
    'do something
End If

If you would like to see if any of the strings in the list are equal to your string, do this

For i = 0 to myList.Count - 1
    If myString = myList(i) Then
        'do something
    End If
Next
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to test whether the list contains "Bob" you can use the Contains Method:

    Dim myList As New List(Of String)
    myList.Add("Bob")
    If(myList.Contains("Bob")) Then
        Console.WriteLine("Yes, it is in the list")
    End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.