Reputation: 59
I am trying to find out if an array of Strings exists in a list of arrays of Strings, but am running into some confusion. Here is some code:
Dim listResults as List(of String)
Dim listStringArrays as List(of String())
Dim Something as String() = {"Foo", "Bar", "Stuff"}
Dim Otherthing as string() = {"Foo", "Bar", "Stuff"}
listStringArrays.Add(Something)
IF listStringArrays.Contains(Otherthing) then
listResults.Add("True")
Else
listResults.Add("False")
End If
IF listStringArrays(0).Equals(Otherthing) then
listResults.Add("True")
Else
listResults.Add("False")
End If
listResults would then contain two "False". But strangely:
Something(0) = Otherthing(0)
Something(1) = Otherthing(1)
Something(2) = Otherthing(2)
These would all evaluate to true. How can I find out if my listStringArrays contains Otherthing if Contains does not work?
Bonus question: Why would Contains not work in this instance?
Upvotes: 1
Views: 3077
Reputation: 103467
Two arrays with the same contents are still not the same array. Something = Otherthing
is false. That's why Contains
doesn't work.
You could instead use SequenceEqual
to see whether any of the arrays in listStringArrays
have the same contents as Otherthing
.
If listStringArrays.Any(Function(t) t.SequenceEqual(Otherthing)) Then
Upvotes: 3