4

Is there an easy way to print an array, which is potentially multi-dimensional, to the Console in VB.NET for the purpose of debugging (i.e. just checking that the contents of the array is correct).

Coming from an Objective-C background the NSLog function prints a reasonably well formatted output, such as the following for a one-dimensional array:

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

and similar for a multi-dimensional array (the following is an example of a two-dimensional array output):

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

1 回答 1

2

我认为没有任何本机(内置)功能可以做到这一点,但是下面的功能应该可以正常工作。

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,当您在控制台应用程序中使用该函数时应该可以省略它。

于 2013-03-22T16:04:17.813 回答