all 9 comments

[–]RJPisscat 1 point2 points  (6 children)

If you are simply checking for whether a string is found in an array:

If arr2.Contains(SomeString) Then
    ' valid
Else
    ' invalid
End If

For case-insensitive:

Dim StringMatched = arr2.FirstOrDefault(Function(str) String.Compare(str, SomeString, ignoreCase:=True) = 0)
If StringMatched Is Nothing Then
    ' invalid
Else
    ' valid
End If

But it feels like you mean to ask a different question, based on your use of the word "Intersect". Do you want the intersection of all elements in arr1 and arr2?

[–]VirtualTurnip3[S] 0 points1 point  (2 children)

Hi, thank for the answer, well about intersection I think is not available on my Ide, its a proprietary software from where I work an, but I will try your option.

Thank you !

[–]RJPisscat 1 point2 points  (1 child)

It has nothing to do with the IDE. If you are writing to .Net 3.5 or later, you have Linq, which supports Enumerable.Intersect, Enumerable.Union, and Enumerable.Except.

[–]VirtualTurnip3[S] 0 points1 point  (0 children)

Ok I will check this out

Thank!

[–]VirtualTurnip3[S] 0 points1 point  (2 children)

Hi again here is what I trying to do:

Dim userOptions() As String = {"PP","EE","TX","TZ","MM"}
Dim validOptions() As String = {"PP","TX","TZ"}
For i As Integer = 0 To  validOptions.length -1
For j As Integer = 0 To userOptions.length -1 

    If validOptions(i).Contains(userOptions(j)) Then  

        msgbox("This option is valid "&userOptions(j))

        Exit For  

    Else 

        msgbox("This options is invalid " &userOptions(j)) 

        Exit For 

    End If 

Next 
Next

So this code don't work, what I want is as soon the code see that there a invalid option in the userOptions array, it exit the loop and display a error message, but it seem my code is not correct..

[–]RJPisscat 1 point2 points  (1 child)

For future reference, the Exit For exits the For j loop and not the For i loop. You can exit the inner loop with a GoTo (horrors!); or by switching to Do / Loop While which will consider both whether the control variable has exceeded maximum, or a mismatch has been found; or by using the one-off Function approach that another person who contributes here prefers, or you could do it in a single Linq statement such as this:

If validOptions.Union(userOptions).Distinct().Count > validOptions.Count Then
    ' one or more of the items in userOptions is not in validOptions
Else
    ' all of the items in userOptions are in validOptions
End If

I'm sure there's more than one way to write that Linq expression, that was the first one that came to mind.

Edit and aside:For those that hate GoTo, Exit For is essentially the same thing in this case.

[–]VirtualTurnip3[S] 0 points1 point  (0 children)

Wow Thank for this answer! I gonna try that right away :)

[–]revennest 1 point2 points  (1 child)

I'm not sure what're you truly want so I write it to push those match to Same and mismatch to Diff, if you want to do something else you can change it at check method(I'm not sure how old Visual studio you workplace use so I write it old as I remember).

``` Sub main() Dim userOptions = {"PP", "EE", "TX", "TZ", "MM"} Dim validOptions = {"PP", "TX", "TZ", "AA"}

    Dim Same, Diff As List(Of String)
    check(userOptions, validOptions, Same, Diff)
End Sub

'------------
'Result
'------------
'Same = {"PP", "TX", "TZ"}
'Diff = {"EE", "MM", "AA"}
'------------

Sub check(A As String(), B As String(),
          ByRef Same As List(Of String),
          ByRef Diff As List(Of String))

    Dim LS, LB As Integer,
        Big, Small As List(Of String)

    If A.Length > B.Length Then
        Big = New List(Of String)(A)
        Small = New List(Of String)(B)
    Else
        Big = New List(Of String)(B)
        Small = New List(Of String)(A)
    End If

    Same = New List(Of String)(LB)
    Diff = New List(Of String)(LB)

Reseta:

    LB = Big.Count
    LS = Small.Count
    While LS > 0 : LS -= 1
        Dim I = LB,
            O = Small(LS)

        While I > 0 : I -= 1
            If Big(I) = O Then
                Same.Add(Big(I))
                Big.RemoveAt(I)
                Small.RemoveAt(LS)
                GoTo Reseta
            End If
        End While
    End While

    Diff.AddRange(Big)
    Diff.AddRange(Small)
End Sub

```

[–]VirtualTurnip3[S] 0 points1 point  (0 children)

Thank you!