1

したがって、ジャグ配列の値を検索する関数があり、次のようになります。

Private Function Lookup(ByVal Search_path As String) As Integer
    Dim i As Integer = 0
    Do Until MasterIndex(i) Is Nothing 'throws an exception here
        If Search_path = MasterIndex(i)(0) Then
            Return MasterIndex(i)(1)
        End If
        i = i + 1
    Loop
    Return -1
End Function

問題は、これを空の配列でテストすると、Index was outside the bounds of the array3行目のエラーが発生することです。これを修正するにはどうすればよいですか?

4

2 に答える 2

3

インデクサーが配列内の要素数を超えているかどうかを確認する必要があります。

Private Function Lookup(ByVal Search_path As String) As Integer
    Dim i As Integer = 0
    Do Until i = MasterIndex.Length OrElse MasterIndex(i) Is Nothing
        If Search_path = MasterIndex(i)(0) Then
            Return MasterIndex(i)(1)
        End If
        i = i + 1
    Loop
    Return -1
End Function
于 2011-10-17T13:44:13.717 に答える