Objects stored in Variant variables
You can store Objects in a Variant variable. This can be useful when you need to efficiently work with a variety of data types, including Objects. For example, all elements in an array must have the same data type. Setting the data type of an array to a Variant allows objects to be stored together with other data types in the same array.
Array
If you have an experience with programming in another language, you must be familiar with the concept of arrays. With arrays, you can refer to a series of variables with the same name and use numbers (indexes) to identify them. In many cases, using arrays can shorten and simplify programs because you can design a loop with indexed values to efficiently handle multiple situations. The array has upper and lower bounds, and the elements of the array are contiguous in the upper and lower bounds. Because Visual basic allocates space for each index value, do not have an unrealistic declaration of a too large array.
Note that the array of discussions in this section is an array of variables declared in the program. They are different from the control array, which is specified at design time by setting the control's Index property. Variable arrays are always contiguous; unlike control arrays, you cannot load or unload an array element from the middle of an array.
All elements in an array have the same data type. Of course, when the data type is variant, each element can contain different kinds of data (objects, strings, numeric values, and so on). You can declare an array of any of the base data types, including user-defined types (see "Creating Your own data Types" in chapter eighth "re-programming") and object variables (see Chapter Nineth, "Programming with Objects").
There are two types of arrays in Visual Basic: fixed-size Arrays--it always stays the same size, and dynamic arrays that can change in size at run time. Dynamic arrays are discussed in more detail in the "Dynamic array" later in this chapter.
Declaring an array of fixed sizes
There are three ways to declare an array of fixed-size size, depending on the valid range the array should have:
Creates a common array that declares the array with the public statement in the declaration segment of the module.
Set up the module series, declare the array in the Declaration section of the module with Private statement.
Creates a local array in which the array is declared with a Private statement.
When
Set bounds
declares an array, the array name is followed by an upper bound enclosed in parentheses. The upper bound must not exceed the range of the Long data type (-2,147,483,648 to 2,147,483,647). For example, the following array declaration can appear in the declaration segment of a module:
Dim counters as Integer ' 15 elements.
Dim sums as Double ' 21 elements.
to create a common array, replace Dim with public directly.
Public counters as Integer
Public sums as Double
uses Dim:dim counters as Integerdim during the same declaration in the procedure sums The first declaration of as double establishes an array of 15 elements, with an index number ranging from 0 to 14. The second declaration establishes an array of 21 elements, with an index number ranging from 0 to 20. The default lower bound is 0.
to explicitly provide the lower bound with the keyword to explicit (a Long data type) to specify the lower bound:
Dim counters (1 to) as Integer
Dim sums as String
in the preceding declaration, Counters index values range from 1 to 15, while sums index ranges from 100 to 120.
An array that contains other arrays
It is possible to create arrays of Variant data types and to reside with arrays of different data types. The following code creates two arrays, one containing integers and the other containing strings. Then declare the third Variant array and place the integer and string array in it:
Private Sub Command1_Click ()
Dim IntX As Integer declares the counter variable.
' declares and places an array of integers.
Dim Countersa (5) as Integer
for IntX = 0 to 4
Countersa (IntX) = 5
Next IntX
' declares and places an array of strings.
Dim COUNTERSB (5) as String
for IntX = 0 to 4
COUNTERSB (IntX) = "Hello"
Next intX
Dim Arrx (2) as Variant ' declares a new array with two members.
Arrx (1) = Countersa () "migrates other arrays to the array."
Arrx (2) = COUNTERSB ()
MsgBox Arrx (1) (2) ' shows the members of each array.
MsgBox Arrx (2) (3)
End Sub
Multidimensional arrays
Sometimes it is necessary to trace the relevant information in the array of records. For example, to track every pixel on a computer screen, you need to reference its X, Y coordinates. In this case, you should store the values in a multidimensional array. You can declare multidimensional arrays in Visual Basic. For example, the following statement declares a two-dimensional array of 10x10 within a procedure.
Static Matrixa (9, 9) as Double
You can declare either two or two dimensions with an explicit lower bound:
Static Matrixa (1 to ten, 1 to ten) as Double
All of these can be extended to arrays above two dimensions. For example:
Dim Multid (3, 1 to ten, 1 to 15)
This declaration establishes a three-dimensional array with a size of 4x10x15. The total number of elements is the product of three dimensions, 600.
Note that when you increase the number of dimensions in an array, the storage space that the array occupies increases dramatically, so use a multidimensional array with caution. Use Variant arrays with extra care because they require a greater storage space.