Printed (multidimensional) dimensional array in Visual Basic

Is there an easy way to print an array that is potentially multidimensional to the console in VB.NET for debugging purposes (i.e. just check the contents of the array are correct).

Based on the Objective-C background, the NSLog function prints a fairly well-formatted output, for example, for a one-dimensional array:

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

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

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

I don’t think there is a built-in function for this (built-in) However, the function below should work fine.

 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 

To test a function in a non-console application, I added the string variable S, which you should overlook when using this function in a console application.

+2
source

Source: https://habr.com/ru/post/1469159/


All Articles