3

Here i have my simple code, containt list of arrays and 2 text boxes, when i press button script must check if text form Textbox2 is found in list of array. Can you help me to fix it ? Thanks !

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pins() As String = {"dgge", "wada", "caas", "reaa"}
    If TextBox2.Text = pins() Then
        TextBox1.Text = "Succes"
    End If End Sub
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
Malasuerte94
  • 1,454
  • 3
  • 14
  • 18
  • `If pins.IndexOf(textBox2.Text) <> -1 Then ... End If` (assuming I remember VB correctly.) p.s. See also: http://stackoverflow.com/a/11112305/298053 – Brad Christie Feb 22 '13 at 20:16

3 Answers3

1

If you want to use LINQ, you can just do this:

If pins.Contains(TextBox2.Text) Then
    TextBox1.Text = "Success"
End If

Otherwise, the easiest option would be to use a List instead of an array:

Dim pins As New List(Of String)(New String() {"dgge", "wada", "caas", "reaa"})
If pins.Contains(TextBox2.Text) Then
    TextBox1.Text = "Success"
End If

But, if you must use an array, you can use the IndexOf method in the Array class:

If Array.IndexOf(TextBox2.Text) >=0 Then
    TextBox1.Text = "Success"
End If
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
0
If Array.IndexOf(pins, TextBox2.Text) <> -1 Then
    TextBox1.Text = "Succes"
End If End Sub
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0
If pins.IndexOf(TextBox2.Text) >= 0 Then
    TextBox1.Text = "Founded"
End If

Or if you use List(Of String) instead of array:

If pins.Contains(TextBox2.Text) Then
    TextBox1.Text = "Founded"
End If
SysDragon
  • 9,692
  • 15
  • 60
  • 89