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
MyArray.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
MyArray.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
MyArray.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
MyArray.Sort(MyArray)
MyArray.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
MyArray.Sort(MyArray)
MyArray.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
All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks |