问题可能是由于您的数组包含整数,但您要查找的值是短的。考虑以下示例:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, value)) ' Prints -1
如果数组包含整数,则需要先将 short 转换为整数,例如,使用CInt
:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' Prints 0
编辑:请注意,声明的类型与此无关。让我们将数组声明为Object,因为这是您在评论中提到的(请注意,以下示例需要 Option Strict Off,这很糟糕):
Dim myArray As Object = New Integer() {5}
Dim value As Object = 5S ' Short literal
Console.WriteLine(Array.IndexOf(myArray, value)) ' still returns -1
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' returns 0
注意:您可以通过将函数声明为Public Sub SomeFunction(ByVal test As Integer)
.