索引数超过索引数组 VB 的维度数



在我调用函数的行的子主中,我收到一个我不知道如何解决的错误:

索引

数超过索引数组的维度数

Module Module1
Sub main()
Dim names() As String = {"heimdall", "hella", "loki", "thor", "tyr", "odin"} 'list in order
Dim last As Integer = names.Length - 1
Dim first As Integer = 0
Dim player As String = "thor"
search(names(), last, first, player)
End Sub
Function search(ByVal names() As String, ByVal last As Integer, ByVal first As Integer, ByVal player As String)
Dim midpoint As Integer = (first + last)  2
Dim found As Boolean = False
While found = False
If last < first Then
Return -1
End If
If names(midpoint) > player Then
Return search(names, last, first, midpoint)
found = True
Else
Return midpoint
found = True
End If
End While

Return midpoint 'automatically does this when found = true
End Function
End Module

names()末尾的括号传递给search方法时将其删除。仅在声明数组时需要这些。

search(names, last, first, player)

最新更新