Jump to content

Visual Basic .NET/Arrays

From Wikibooks, open books for an open world

Simple arrays

[edit | edit source]

An array is simply a variable that can store more than one piece of data. The data is stored in a list. If you declare an integer, then that variable can only store one integer. An array of integers can store many integers. Each one is given its own number.
For example, this line of code:

 Dim MyArray(5) As Integer

will give an array like this

Index Data
00 Nothing
01 Nothing
02 Nothing
03 Nothing
04 Nothing
05 Nothing
 Dim arrayName(upperBound) As arrayType

This is similar to declaring a normal variable with one difference, the upperBound argument. Unlike previous versions versions of Visual Basic, all arrays in Visual Basic .NET start at 0 and go to the upperBound, for Example:

 Dim MyArray(10) As Integer

will declare a variable called MyArray with space for 11 Integers. The integers are stored in MyArray(0), MyArray(1),..., MyArray(10).

To declare an array and assign values in a single line, use the following

 Dim MyArray() As Integer = New Integer(4) { 1, 2, 3, 4, 5 }

Another way to create an array is as follows

 Module Module1
   Sub Main()

       Dim MyArray As System.Array
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       MyArray(0) = "a"
       MyArray(1) = "b"
       MyArray(2) = "c"
       MyArray(3) = "d"
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()

   End Sub
 End Module

Note that anytime these lines are used:

 Dim MyArray As System.Array
 MyArray = System.Array.CreateInstance(GetType(String), 4)

This means the same thing, and is easier to use:

 MyArray = System.Array.CreateInstance(GetType(String), 4)

The (number) after the name of the variable declares it as a string.

To access one of the elements in the array:

 Module Module1
   Sub Main()

       Dim MyArray As System.Array
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       MyArray(0) = "a"
       MyArray(1) = "b"
       MyArray(2) = "c"
       MyArray(3) = "d"
       Console.WriteLine(MyArray.GetValue(2)) 'returns c
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()

   End Sub
 End Module

How many elements do we have in the array?

 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       MyArray(0) = "a"
       MyArray(1) = "b"
       MyArray(2) = "c"
       MyArray(3) = "d"
       Console.WriteLine(MyArray.Length)
       'displays 4
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
       '    
   End Sub
 End Module

Display all the elements in the array.

 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray(0) = "a"
       MyArray(1) = "d"
       MyArray(2) = "b"
       MyArray(3) = "c"
       En = MyArray.GetEnumerator
       '
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
 End Module

Sort the array.

 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray(0) = "a"
       MyArray(1) = "d"
       MyArray(2) = "b"
       MyArray(3) = "c"
       En = MyArray.GetEnumerator
       Console.WriteLine("Before sorting")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       Array.Sort(MyArray)
       En = MyArray.GetEnumerator
       Console.WriteLine("After sorting")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
 End Module

Sort in descending order.

 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       Dim DescSortCompare = New DescSortCompareClass
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray(0) = "a"
       MyArray(1) = "d"
       MyArray(2) = "b"
       MyArray(3) = "c"
       En = MyArray.GetEnumerator
       Console.WriteLine("Before descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       Array.Sort(MyArray, DescSortCompare)
       En = MyArray.GetEnumerator
       Console.WriteLine("After descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
   '
   Public Class DescSortCompareClass
       Implements IComparer
       '
       Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
          Implements IComparer.Compare
           Return x > y
       End Function
       '
   End Class
 End Module

Reverse the array.

 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray(0) = "a"
       MyArray(1) = "d"
       MyArray(2) = "b"
       MyArray(3) = "c"
       En = MyArray.GetEnumerator
       Console.WriteLine("Before reversing the array")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       Array.Reverse(MyArray)
       En = MyArray.GetEnumerator
       Console.WriteLine("After reversing the array")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
   '
 End Module

Now that we have Reverse, here is another way to sort in descending order without IComparer.

 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray(0) = "a"
       MyArray(1) = "d"
       MyArray(2) = "b"
       MyArray(3) = "c"
       '
       En = MyArray.GetEnumerator
       Console.WriteLine("Before descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       Array.Sort(MyArray)
       Array.Reverse(MyArray)
       En = MyArray.GetEnumerator
       Console.WriteLine("After descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
 End Module

With Option Strict On, we would need to use SetValue to assign array elements.

 Option Strict On
 Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray.SetValue("a", 0)
       MyArray.SetValue("d", 1)
       MyArray.SetValue("b", 2)
       MyArray.SetValue("c", 3)
       '
       En = MyArray.GetEnumerator
       Console.WriteLine("Before descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       Array.Sort(MyArray)
       Array.Reverse(MyArray)
       En = MyArray.GetEnumerator
       Console.WriteLine("After descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
 End Module

Multi-Dimensional arrays

[edit | edit source]

Arrays also can be multi-dimensional - 2D, 3D, 4D etc. For example, this line of code:

 'Declare an 2D Array with integers(X, Y)
 Dim My2DArray(2, 2) As Integer

Will give an array like this:

Index Data
0, 0 Nothing
0, 1 Nothing
0, 2 Nothing
1, 0 Nothing
1, 1 Nothing
1, 2 Nothing
2, 0 Nothing
2, 1 Nothing
2, 2 Nothing

2D arrays can be used in games like a Tic-Tac-Toe.

3D arrays can be thought of as an array of 2D arrays. Where a 2D array has rows and columns, as in a game of Tic Tac Toe, the 3D array stores multiple grids of Tic Tac Toe.

To declare a 3D array:

 'Declare an 3D Array of integers(X, Y)
 Dim My3DArray(1, 2, 2) As Integer

In this example, the first dimension would typically be the number of arrays in this 3D array. We know these are 2D arrays as the second dimension specifies how many rows in these arrays and the third dimension the number of columns.

Arrays manipulations methods

[edit | edit source]
  • Split() : to transform a string into an array, according to the separator which is placed in parameter.
  • Join() : to convert an array into a string.
  • Filter() : to filter some array entries.

Example:

 Module Module1
   Sub Main()
     Dim ArrayName() As String = Split("lorem ipsum dolor sit", " ")
     Console.WriteLine(ArrayName(1))  ' displays "ipsum"

     ArrayName(4) = "amet"
     Dim ArrayContent As String = String.Join(" ", ArrayName)
     Console.WriteLine(ArrayContent)

     Dim SubArray = Filter(ArrayName, "o", True, CompareMethod.Text)
     Console.WriteLine(String.Join(" ", SubArray))  ' displays the words including "o"

     Console.ReadLine()
   End Sub
 End Module
[edit | edit source]