-3

I am getting this error while trying to use a linear search to look for a name entered by the user into an array. Here is when I declare the array and get the input.

Public Const SIZE_ARRAY = 9

Public Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click

Dim myArray(SIZE_ARRAY) As String
Dim index As Integer
Public Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
    Dim count As Integer = 0

    For Me.index = 0 To SIZE_ARRAY
        myArray(index) = InputBox("Enter a name, Enter a name")
        count = count + 1
    Next

If count = 10 Then
        lblInstructions.Visible = False
        cmdStart.Visible = False
        lblInstructions2.Visible = True
        txtSearch.Visible = True
        lblOutput.Visible = True
        cmdSearch.Visible = True
    End If
End Sub

Here is where I use the linear search.

Public Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
    Dim found As Boolean
    Dim name As String

    name = txtSearch.Text

    found = LinearSearch(myArray, Val(name))

    If found Then
        lblOutput.Text = name & " was found at cell " & index
    Else
        lblOutput.Text = name & " was not found"
    End If

End Sub

And here is the linear search function

Public Function LinearSearch(ByVal list() As Integer, ByVal searchValue As Integer) As Boolean
    Dim found As Boolean = False
    Dim index As Integer
    While found = False And index <= UBound(list)
        If list(index) = searchValue Then
            found = True
        Else
            index += 1
        End If
    End While
    Return found
End Function
user3029128
  • 127
  • 1
  • 1
  • 7
  • 1
    What part of the error message didn't you understand? – Raymond Chen Apr 20 '16 at 02:33
  • Just as a small comment - In the Sub that gets the user to input names - the `count` variable will **always** equal 10 after the loop has finished. The `If..End If` statements around the subsequent block of code are unnecessary. - Cheers – David Wilson Apr 20 '16 at 19:22
  • Also - Sorry - I noticed that you want to use `index` in your cmdSearc_click sub to show where the name was found. However the `index` variable in your `LinearSearch` sub - although it has has the same name name, isn't the same variable. So you won't get the correct index from linear search. – David Wilson Apr 20 '16 at 19:29

1 Answers1

0

myArray is a String array, but your method LinearSearch expects two arguments: an Integer array and an Integer. I assume you require the array to search to be of type String, and a search value that is also of type String

My VB.NET is rusty, but here is some untested code for a linear search.

Public Function LinearSearch(ByVal list() As String, ByVal searchValue As String) As Boolean
        For Each item As String In list
            If item = searchValue Then
                 Return True
            End If 
        Next
        Return False
End Function  
TEK
  • 1,265
  • 1
  • 15
  • 30
  • Is there something wrong with this answer? Just because you don't like OPs question, doesn't mean you down vote answers. Explain yourself. – TEK Apr 20 '16 at 02:36
  • I'm not sure why this answer was downvoted either. It describes the exact problem and provides a solution. – Chris Dunaway Apr 20 '16 at 13:37