Results 1 to 8 of 8

Thread: InvalidArgument value of '1' is not valid for 'index' Help!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2015
    Posts
    5

    InvalidArgument value of '1' is not valid for 'index' Help!!!

    When the user input the customer name and then click the button “find”, your program can search the customer history record by the customer name. If the customer name is found in the history record, the message box will be shown "__ is on line _ of the list."

    If the customer name cannot be found in the history record, the message will be shown "Customer not found".

    Code:
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
            Dim i As Integer
    
            For i = 1 To 999
                If lstCusHis.Items(i) = txtFind.Text Then
                    MsgBox(txtFind.Text & " is on line " & i + 1 & " of the list.")
                End If
                If lstCusHis.Items(i) <> txtFind.Text Then
                    MsgBox("Customer not found")
                End If
    
            Next
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    Your code is wrong in a number of ways. With regards to the issue, this:
    Code:
    For i = 1 To 999
    should be this:
    Code:
    For i = 0 To lstCusHis.Items.Count - 1
    You should always base the limits of a For loop on the limits of the list you're enumerating. The first index in any array or collection is 0, not 1, the the last index is based on the Count or Length.

    Another is the fact that you are telling the user that their item is not found for EVERY item in the list that doesn't match. That means that if their entry matches the last itme in the list, you will tell them that there entry was not found once for every other item in the list. You need to get that second message OUT of the loop and ONLY display it if a match is not found anywhere in the list.

    Finally, you are continuing to look for a match even after you find one. If there are hundreds of entries and you match the first one, what's the point of testing the rest?

  3. #3
    Member
    Join Date
    Nov 2011
    Posts
    52

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    Quote Originally Posted by katrina97 View Post
    When the user input the customer name and then click the button “find”, your program can search the customer history record by the customer name. If the customer name is found in the history record, the message box will be shown "__ is on line _ of the list."

    If the customer name cannot be found in the history record, the message will be shown "Customer not found".

    Code:
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
            Dim i As Integer
    
            For i = 1 To 999
                If lstCusHis.Items(i) = txtFind.Text Then
                    MsgBox(txtFind.Text & " is on line " & i + 1 & " of the list.")
                End If
                If lstCusHis.Items(i) <> txtFind.Text Then
                    MsgBox("Customer not found")
                End If
    
            Next
        End Sub

    Hello katrina,

    what is the lstCusHis? Is it a listbox?

    If so, you can directly use the following without any For Loops:

    Code:
    If lstCusHis.Items.Contains(txtFind.Text) Then
    MsgBox(String.Format ("{0} is on line {1}", txtFind.Text, lstCusHis.Items.IndexOf(txtFind.Text).ToString()))
    End If

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    Quote Originally Posted by tassoma View Post
    Hello katrina,

    what is the lstCusHis? Is it a listbox?

    If so, you can directly use the following without any For Loops:

    Code:
    If lstCusHis.Items.Contains(txtFind.Text) Then
    MsgBox(String.Format ("{0} is on line {1}", txtFind.Text, lstCusHis.Items.IndexOf(txtFind.Text).ToString()))
    End If
    Quite so, but it's rather inefficient to call Contains and IndexOf. It would be more efficient to call just IndexOf and then test whether that is -1 or not. Also, there's no need to call ToString on something that's being passed as an argument to String.Format. That is done implicitly.

  5. #5
    Member
    Join Date
    Nov 2011
    Posts
    52

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    Yes, you are right jmcilhinney .. IndexOf only will be efficient.

    Also no need for ToString() .. it takes Object argument ..

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2015
    Posts
    5

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    Quote Originally Posted by tassoma View Post
    Hello katrina,

    what is the lstCusHis? Is it a listbox?

    If so, you can directly use the following without any For Loops:

    Code:
    If lstCusHis.Items.Contains(txtFind.Text) Then
    MsgBox(String.Format ("{0} is on line {1}", txtFind.Text, lstCusHis.Items.IndexOf(txtFind.Text).ToString()))
    End If

    lstCusHis is a listbox.

    It must be For...Next statement.
    It doesn't work.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    Quote Originally Posted by katrina97 View Post
    lstCusHis is a listbox.

    It must be For...Next statement.
    It doesn't work.
    If you have to use a For loop then why even try something that doesn't contain a For loop? I told you how to modify your code to work with a For loop in my first post. If you have to use a For loop then maybe you should try that.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2015
    Posts
    5

    Re: InvalidArgument value of '1' is not valid for 'index' Help!!!

    problem fixed.
    thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width