1
Private Sub txtQty_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.GotFocus
   Dim strItem As String
   strItem = txtItem.Text
   Dim strArray As String
   strArray = itemArr(1)

   If String.Compare(strItem, strArray) = True Then
       MessageBox.Show("item in array!")
   End If
End Sub

Private Sub txtQty_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.TextChanged

    If txtItem.Text <> Nothing And txtQty.Text <> Nothing Then

        'create rows in the DataTable
        tblScItem.Rows.Add(itemArray())
    End If

    txtItem.Text = ""
    txtQty.Text = ""

End Sub

This is how i declare my array:

Function itemArray() As String()

        itemArr(0) = ""
        itemArr(1) = txtItem.Text
        itemArr(2) = Form2.cbGondola.SelectedItem
        itemArr(3) = txtQty.Text
        itemArr(4) = DateTime.Now
        itemArr(5) = Form1.txtLoginId.Text

        Return itemArr
End Function

I dont seem to do a proper check, help!

3
  • 2
    Dim strArray As String. What a horrible naming. That not an array but a string. Commented Sep 26, 2012 at 10:17
  • 1
    Where do you declare itemArr? Commented Sep 26, 2012 at 10:20
  • @Guffa I declared in the itemArr in: Module globalVariable Public itemArr(5) As String End Module Commented Oct 9, 2012 at 8:15

1 Answer 1

1

The String.Compare method doesn't return a boolean, it returns an integer.

If the strings are equal, it returns 0.

If String.Compare(strItem, strArray) = 0 Then

You should set Option Strict to On in your project, so that the compiler won't allow the implicit conversion from boolean to integer.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for Option Strict! However, all that really needs to be done is If txtItem.Text = itemArr(1) Then
@StevenDoggart I will try both methods and see how it goes, thanks!
@user1699905: Using the = operator also works. I just wanted to show exactly why the code that you have doesn't work as you expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.