I am doing a task to search for data in an array, in this example I have an array with values 01, 02, and 03.
What I do is place a value in a text box and in a function FindKeys indicate if that value exists in the array. I don't know where I could be wrong since it only takes the first value of the array and therefore it always prints the same message.
This is the code I have.
Dim VectorA() As String = {"01", "02", "03"}
Public Function buscarClaves(ByVal dts As String) As Boolean
'buscarClaves = False
For Each clave As String In VectorA
If clave = dts Then
Return True
Else
Return False
End If
Next
'
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 1 To VectorA.Length
If buscarClaves(TextBox1.Text) Then
MsgBox("Clave duplicado")
Else
MsgBox("Clave disponible")
End If
Next i
End Sub
The for in the Button event is superfluous, since you have a For Each that runs through the entire array in the function, that's why it repeats the same value in the message, because it always finds the key in the three runs it makes through the array length.
And I made some changes to the findKeys() function. If it finds the value it returns true, otherwise it will just take the value you gave it at the beginning, it is not necessary to add the else with the return false.