数组 – 在Visual Basic中打印(多)维数组

有没有一种简单的方法可以在VB.NET中向控制台打印一个可能是多维的数组,以便进行调试(即只检查数组的内容是否正确).

来自Objective-C背景的NSLog函数打印出格式合理的输出,如下面的一维数组:

myArray {
    0 => "Hello"
    1 => "World"
    2 => "Good Day"
    3 => "To You!"
}

和类似的多维数组(以下是二维数组输出的例子):

myTwoDArray {
    0 => {
        0 => "Element"
        1 => "Zero"
    }
    1 => {
        0 => "Element"
        1 => "One"
    }
    2 => {
        0 => "Element"
        1 => "Two"
    }
    3 => {
        0 => "Element"
        1 => "Three"
    }
}

最佳答案 我不认为有任何本机(内置)功能,

但是下面的功能应该可以正常工作.

Public Shared Sub PrintValues(myArr As Array)
  Dim s As String = ""
  Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
  Dim i As Integer = 0
  Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
  While myEnumerator.MoveNext()
    If i < cols Then
      i += 1
    Else
      'Console.WriteLine()
      s = s & vbCrLf
      i = 1
    End If
    'Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
    s = s & myEnumerator.Current & " "
  End While
  'Console.WriteLine()
  MsgBox(s)
End Sub

为了在非控制台应用程序中测试函数,我添加了字符串变量S,当您在控制台应用程序中使用该函数时,您应该可以省略它.

点赞