why does array.contains not work with array returned from function?

polynaux

Member
Joined
Apr 3, 2007
Messages
11
Programming Experience
Beginner
Hi Guys,

I a function that returns an array and would like to check if this array contains
a certain string on rowdatabound of my gridview:

problema9.JPG


But.....contains is not recognized.
why does array.contains not work with array returned from function?

Many thanks

polynaux
 
It might be because of the parenthesis, try removing them from between the 'PersonalContactsArray' and the '.Contains'
 
If you do it using a List, it works fine :)

VB.NET:
    Private Function PersonalContactsList() As List(Of String)
        Dim PersonalContacts As New List(Of String)
        PersonalContacts.Add("Person0")

        For i As Integer = 1 To 10
            PersonalContacts.Add(String.Format("Person{0}", i))
        Next

        Return PersonalContacts
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If PersonalContactsList.Contains("Person1") = True Then
            MessageBox.Show("Yes")
        Else
            MessageBox.Show("No")
        End If

        If PersonalContactsList.Contains("Person37") = True Then
            MessageBox.Show("Yes")
        Else
            MessageBox.Show("No")
        End If

    End Sub
 
It is correct that System.Array class doesn't have a Contains method, but collections (IList) does as mentioned. Arrays have IndexOf method that can also be used, or Exists method too.
 
ok i see

hmm its strange because I have used the contains method in the same project (just another code behind file):

VB.NET:
          Dim Lengths As Integer = GridViewAlternativeAssessmentForUserID.Rows.Count
                Dim AssessmentCriteriaArray(Lengths) As String
          
               

                                    If AssessmentCriteriaArray(Lengths).Contains(xxxx.Text) THEN....

and it works perfecty.
Now you guys tell me there is no contains method. odd.
I will try the it with lists - I have never used lists before so is there anything else I need to be aware of?

many thanks
polynaux
 
You have used the String.Contains method. Like If "some text".Contains("text") ;)
 
Back
Top