Array Declaration and Built-in Functions
This chapter describes:
- What Is an Array?
- Array Variable Declaration
- Assigning Values to Array Elements
- Retrieving Values from Array Elements
- Fixed-Size Array Example
- Dynamic-Size Array Example
- "Array" Function and "For Each" Example
- "Erase" Statements
What Is an Array?
Like many other programming languages, Visual Basic support the concept of array as a built-in data structure
with the following features:
- An array can be used to store a collection of data elements of the same data type.
- The number of elements in an array can be a fixed number or can be re-adjusted dynamically.
- An array must be associated with a variable for referencing.
- Each element in an array is associated with a unique index number. By default, index number starts from 0.
- A specific element in an array can be referred by the index number.
- A number of built-in functions are provided to work with arrays.
- "For Each" loop statement is provided as an easy way to loop through all elements in an array.
- "Erase" statement is provided as a quick way to remove all values from an array.
Array Variable Declaration
Visual Basic supports a number of ways to declare array variables:
1. Explicit Declaration with Fixed Size - Using "Dim" statement with size included as:
Dim variable_name(upper_bound) [As data_type]
where "upper_bound" is positive integer specifying the upper bound of the element index,
and "data_type" is a data type keyword.
Array declared in this way has a fixed size (number of elements).
By default, the size of an array is the upper bound plus 1, because index starts from 0.
This size can not be changed. If "As data_type" is not specified, default data type "Variant" will be used.
2. Explicit Declaration with Dynamic Size - Using "Dim" and "ReDim" statements as shown in the following
Dim variable_name() [As data_type]
ReDim [Preserve] variable_name(upper_bound)
The "Dim" statement declares an array variable without any upper bound. The "ReDim" statement resets
the upper bound to a new value. The optional key word "Preserve" specifies that all old elements
must be preserved when resetting the array size. Array declared in this way can be resized at any time.
3. Implicit Declaration with "Array" Function - Using "Array" function as shown in the following
variable_name = Array(element_1, element_2, ...)
The "Array" function takes a list of values of "Variant" type, and returns a dynamic-size array.
Note that once an array is declared, all elements will have default values based. See other chapter
for different default values for different data types.
VB offers some other useful functions and statements for you to work with arrays:
- IsArray(variable_name) - Returns "True" if the specified variable is an array.
- UBound(array_variable) - Returns the upper bound of the specified array.
- LBound(array_variable) - Returns the lower bound of the specified array.
- "For Each" - Loops through all elements in an array.
- "Erase" - Removes all values from an array.
See sections below for details and examples on those functions and statements.
(Continued on next part...)