Back in the old days, arrays used to be one of my favorite tools. Then .NET came along and I was so enamored with many of the cool things in the System.Collections namespace (System.Collection.Hashtables, System.Collections.ArrayLists, System.Collection.Queues to name a few) that I forgot all about my good old buddy the Array. Well, I've recently decided to rekindle our friendship...
The first thing you'll need to know if you are coming from VB6 is that Array's are now 0 based. You can still use non-zero based arrays if you really want to by calling the NewArray function which is provided in the Compatibility library but don't do it. There are many good reasons to not do it and only one (not very good one)to use it (I don't want to change). So, let's pretend it doesn't exist.
To declare an array, it's very easy:
| Dim ArrayOfInts(10) As Integer |
| ArrayOfInts(0) = 1 ArrayOfInts(1) = 2 ....until 10 |
| Dim ArrayOfInts() As Integer = {1,2,3,4,5,6,7,8,9} |
| Dim ArrayOfInts() as Integer ArrayOfInts = New Integer() {1,2,3,4,5,6,7,8,9} |
| Dim x as Integer = MyArrayOfInts(0) |
| Dim ArrayOfInts as Integer() |
| Dim i As Integer For i = 0 to MyArrayOfInts.Length-1 Debug.WriteLine(MyArrayOfInts(i) Next |
| For i as Integer = 0 to MyArrayOfInts.Length-1 Debug.WriteLine(MyArrayOfInts(i) Next |
| Dim i As Integer For Each i in MyArrayOfInts Debug.WriteLine(i) Next |
| 'Would return 8 as the position where it exists if the array 'were initialized as shown above Array.BinarySearch(MyIntArray, 9) |
| Array.Sort(MyIntArray) |
| Array.Reverse(MyIntArray) |
| Array.Clear(MyIntArray, 0, 1) |
| Array.Copy(MyIntArray, MySecondIntArray, 2) |
| MySecondIntArray = MyIntArray.Clone |
| MyArrayList = Array.CopyTo(MyIntArray, 0) |
| 'Will return the value of the 9th element if there is one. Dim i as Integer = MyIntArray.GetValue(9) |