Just as you can initialize variables in the same line in which you declare them, you can initialize arrays, too, with the following constructor (an array initializer, as it’s called):
Dim arrayname() As type = {entry0, entry1, ... entryN}
Code language: VB.NET (vbnet)
Here’s an example that initializes an array of strings:
Dim Names() As String = {"Joe Doe", "Peter Smack"}
Code language: VB.NET (vbnet)
This statement is equivalent to the following statements, which declare an array with two elements and then set their values:
Dim Names(1) As String
Names(0) = "Joe Doe"
Names(1) = "Peter Smack"
Code language: VB.NET (vbnet)
The number of elements in the curly brackets following the array’s declaration determines the dimensions of the array, and you can’t add new elements to the array without resizing it. If you need to resize the array in your code dynamically, you must use the ReDim statement, as described in the section called ‘‘Dynamic Arrays,” later in this chapter. However, you can change the value of the existing elements at will, as you would with any other array.
Array Limits
The first element of an array has index 0. The number that appears in parentheses in the Dim statement is one fewer than the array’s total capacity and is the array’s upper limit (or upper bound). The index of the last element of an array (its upper bound) is given by the method GetUpperBound, which accepts as an argument the dimension of the array and returns the upper bound for this dimension. The arrays we examined so far are one-dimensional and the argument to be passed to the GetUpperBound method is the value 0. The total number of elements in the array is given by the method GetLength, which also accepts a dimension as an argument. The upper bound of the following array is 19, and the capacity of the array is 20 elements:
Dim Names(19) As Integer
Code language: VB.NET (vbnet)
The first element is Names(0), and the last is Names(19). If you execute the following statements, the highlighted values will appear in the Output window:
Debug.WriteLine(Names.GetLowerBound(0))
0
Debug.WriteLine(Names.GetUpperBound(0))
19
Code language: VB.NET (vbnet)
To assign a value to the first and last element of the Names array, use the following statements:
Names(0) = "First entry"
Names(19) = "Last entry"
Code language: VB.NET (vbnet)
If you want to iterate through the array’s elements, use a loop like the following one:
Dim i As Integer, myArray(19) As Integer
For i = 0 To myArray.GetUpperBound(0)
myArray(i) = i * 1000
Next
Code language: VB.NET (vbnet)
The actual number of elements in an array is given by the expression myArray.GetUpperBound(0) + 1. You can also use the array’s Length property to retrieve the count of elements. The following statement will print the number of elements in the array myArray in the Output window:
Debug.WriteLine(myArray.Length)
Code language: VB.NET (vbnet)
Still confused with the zero-indexing scheme, the count of elements, and the index of the last element in the array? You can make the array a little larger than it needs to be and ignore the first element. Just make sure that you never use the zero element in your code — don’t store a value in the element Array(0), and you can then ignore this element. To get 20 elements, declare an array with 21 elements as Dim MyArray(20) As type and then ignore the first element.