Visual Basic .NET/Arrays

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
This page does not follow this book's style guide

[edit] Simple arrays

An array is simply a variable that can store more then 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 


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

[edit] Multi-Dimensional arrays

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.

Personal tools
Create a book