Chapter 11 Quick Reference
To | Do this |
Create an array | Dimension the array by using the Dim keyword. For example:
Dim Employees(9) As String |
Create a public array | Dimension the array by using the Public keyword in a module. For example:
Public Employees(9) As String |
Create a public array specifying upper and lower bounds | Dimension the array as above, but also use the To keyword. For example:
Public Employees(0 To 9) As String Note: The lower bound of the array must always be zero (0). As a result, this syntax is primarily useful for code readability (and is not supported in Visual Basic .NET 2002 and 2003). |
Assign a value to an array | Specify the array name, the index of the array element, and the value. For example:
Employees(5) = "Leslie" |
Format text strings with carriage return and tab characters | Use the vbCrLf and vbTab constants within your program code. (To add these values to strings, use the & operator.) |
Create a dynamic array | Specify the name and type of the array at design time, but omit the number of elements. (If the array has multiple dimensions, insert commas but no numbers between the dimensions.) While your program is running, specify the size of the array by using the ReDim statement. For example:
ReDim Temperatures(10) |
Process the elements in an array | Write a For…Next loop that uses the loop counter variable to address each element in the array. For example:
Dim i As Short Dim Total As Single For i = 0 To UBound(Temperatures) Total = Total + Temperatures(i) Next |
Redimension an array while preserving the data in it | Use the Preserve keyword in your ReDim statement. For example:
ReDim Preserve myCube(25, 25, 50) |
Reorder the contents of an array | Use methods in the Array class of the .NET Framework. To sort an array named RandArray in ascending order, use the Array.Sort method as follows:
Array.Sort(RandArray) To reverse the order of an array named RandArray, use the Array.Reverse method as follows:
Array.Reverse(RandArray) |
To give the user visual feedback during long calculations | Add a ProgressBar control to your form. (You can find the ProgressBar control on the Common Controls tab of the Toolbox.) Set the Minimum, Maximum, and Value properties for the control by using program code. The counter variable in a For…Next loop often offers a good way to set the Value property. |