VB检查是否存在数组索引

时间:2018-09-29 19:12:10

标签: arrays vb.net integer

我在Visual Basic中做一个控制台应用程序只是为了好玩,它有一些命令,但是当我尝试像这样检查索引时:if(cameraZoom == null){ cameraZoom = Camera.open(); } sinse它并不存在,只是错误。是否有检查该功能是否存在的功能?谢谢。

2 个答案:

答案 0 :(得分:0)

只需检查数组的长度。如果您的索引大于数组的大小,则它不能在数组中。

Dim words = New String(){"One", "Two"}
Dim index = 1
If words.Length > index Then 
    Console.Write(words(index))
End If

答案 1 :(得分:-1)

   1: If (Not Words(x) Is Nothing) Then
   2: If Words.Length > 1 Then

OR

Private Sub myFunction()
     Dim Words() As Integer = {1, 2, 3, 4}
     Dim valueExists As Boolean = Array.Exists(Words, New Predicate(Of Integer)(AddressOf compareFunction))
End Sub

Private Function compareFunction(ByVal value As Integer) As Boolean
     Return (value = 4)  'Write your value to check'
End Function