kazinix
kazinix

Reputation: 30093

VB.NET - Alternative to Array.Contains?

Before, I use this on .NET Framework 3.5 and it's working fine:

    If (New String() {"ER", "PM", "EM", "OC"}).Contains(Session("Position")) Then
        'Some codes
    End If

Now I am doing a project that runs with .NET 2.0 and the code above is not working, it's giving me this:

'Contains' is not a member of 'System.Array'.

How can I achieve the codes above (.Contains) without migrating from 2.0 to 3.0? Any alternatives?

Upvotes: 3

Views: 15387

Answers (2)

Koen
Koen

Reputation: 2571

You can achieve this with Exists or Find

If Array.Exists(New String() {"ER", "PM", "EM", "OC"}, AddressOf FindExistance) Then
    'Some codes   
End If



Private Function FindExistance(ByVal s As String) As Boolean
    If String.Equals(s, Session("Position")) Then
        Return True
    Else
        Return False
    End If
End Function

Upvotes: 2

jmoreno
jmoreno

Reputation: 13551

You will have to rewrite your code, like so...

If (Array.IndexOf(New String() {"ER", "PM", "EM", "OC"}), Session("Position")>-1) Then
        'Some codes
End If

The collection initializer is depends on the compiler, but not the framework being targetted, so this should work.

Edit: fixed wrong method/ condition. I got interupted with a leaky sink as I was working on this, and didn't mean to post it until I'd verified that it works.

http://ideone.com/i84QX

Upvotes: 7

Related Questions