VB.NET-索引数小于索引数组的维数

时间:2018-10-05 09:22:33

标签: vb.net

Sub WriteMap()
    Dim CharPos = {1, 1}
    Dim PrintMap As String = ""
    Dim Map(,) As String = {{" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}}

    While True
        Map(CharPos(0))(CharPos(1)) = "X"

        For y = 0 To Map.GetUpperBound(0)
            For x = 0 To Map.GetUpperBound(1)
                PrintMap += $"{Map(y, x)} "
            Next
            PrintMap += vbLf
        Next
        Console.Write(PrintMap)

        Dim Input = Console.ReadKey()
        Console.Clear()
        PrintMap = ""
    End While
End Sub

错误来自Map(CharPos(0))(CharPos(1))=“ X” 我不知道是什么原因造成的,因为Map.Rank()返回2 我一直在寻找解决方案,但到目前为止,只找到了一种打印数组内容的方法,但是我找不到一种方法来编辑数组中的现有条目。

1 个答案:

答案 0 :(得分:4)

您已将Map声明为2D数组,但您像锯齿数组一样访问它。这个:

Map(CharPos(0))(CharPos(1)) = "X"

应该是这样:

Map(CharPos(0), CharPos(1)) = "X"

您将其视为一维数组的一维数组,而二维数组则不是。要将Map声明为锯齿状数组,请执行以下操作:

Dim Map(,) As String

必须是这个:

Dim Map()() As String