我在尝试使用线性搜索查找用户输入数组的名称时收到此错误。这是我声明数组并获得输入的时候。
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
这是我使用线性搜索的地方。
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
这是线性搜索功能
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
答案 0 :(得分:0)
myArray
是String
数组,但您的方法LinearSearch
需要两个参数:Integer
数组和Integer
。我假设您要求数组搜索的类型为String
,搜索值的类型为String
我的VB.NET很生疏,但这里有一些未经测试的线性搜索代码。
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