Arrays in Visual basic
By definition, an array is a list of variables, all with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item.
For example, if we need to enter one hundred names, instead of declaring one hundred different variables, we need to declare only one array. We differentiate each item in the array by using subscript, the index value of each item.
We could use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure.
The general format to declare an array is as follows:
Dim arrayName(subs) as dataType
where subs indicates the last subscript in the array.
Dim CustName(10) as String
will declare an array that consists of 10 elements if the statement Option Base 1 appear in the declaration area, starting from CustName(1) to CustName(10). Otherwise, there will be 11 elements in the array starting from CusName(0) through to CusName(10)
Dim Count(100 to 500) as Integer declares an array that consists of the first element starting from Count(100) and ends at Count(500)
The only data limitation of arrays is that all the elements of the array must be of the same data type. If this limitation is a problem to you, a collection might be a better solution than an array. Of course, when working with an array of variants, each element of the array can hold a different data type value. However, the extra memory and processing requirements of using variants in an array limit this approach.
Declaring a variable-size array requires syntax similar to fixed-length arrays.
Dim ArrayName ( ) [As DataType]
In his case, the UpperBound and LowerBound are purposely omitted. Visual Basic recognizes this declaration as a variable-size array and expects you to eventually establish the array size with the Redim statement:
Redim ArrayName ([LowerBound to [UpperBound]])
As soon as the Redim statement is applied, Visual Basic allocates as much memory as required to contain the array. You can Redim an array multiple times, dynamically altering the amount of memory occupied by the array.
The bounds of an array can be determined with the UBound (upper bound) and LBound (lower bound) functions. The syntax of these functions is as follows:
Because the bound values of arrays are long integers, both of these functions return long values.
You might use fixed and dynamic arrays in an application. Notice how the dynamic arrays are set to specific sizes by the Redim statement before they are used. Notice also that, because the Redim statements do not include the LowerBound specifier, each of the dynamic arrays begins at index 0.
ReDim Preserve astrInt2 (1 to 15)
ReDim Preserve astrStr2 (1 to 15)
The Preserve statement instructs Visual Basic to create a new array with the bounds specified in the Redim statement and then copy the contents of the original array into the new array.
Now that you know what an array is, figuring out what a multidimensional array is will be a little easier. A multidimensional array is an array that looks like a table. If you have ever used Microsoft Excel, you would know . It can also be refered to an array of arrays. That is many arrays using the same name. Here is an example of a two-dimensional array.
Static iArray(1 To 2, 1 To 3)
All the elements list out are:
iArray(1,1), iArray(1,2), iArray(1,3), iArray(2,1), iArray(2,2), iArray(2,3)
Most of the time you will be using one dimensional arrays but some of the time it will be helpful to use 2 or even 3. More then 3 is not always a good idea because it tends to be hard to debug. The usefulness of VB will allow you to use as many as you want, each seperated by commas. Keep in mind the more you use, the more computer memory you use up. This can get real dangerous real fast. If you have some free time someday go ahead and make and 14-15 dimensional array. Make sure you close out and save everything else before hand. See how well your computer runs. Be prepared to have to restart.
|All times are GMT +5.5. The time now is 22:02.|