The VB 6 arrays hold data of a single type. In this article, you will learn about declaring, assigning, and various array handling techniques.
Types of VB 6 Array
You can divide VB 6 arrays into two sets – array and arrays The can have a maximum of dimensions. The arrays in are also classified based on keywords used to declare them:
- – It is declared using keyword whole values remain variable.
- – A standard array can be expanded for more space; hence, it is called dynamic array. You can use keyword – to redeclare the array.
- – A static variable does change its value after exiting a procedure in which they were declared. static arrays do the same and retain their value between procedure calls.
- – The scope is limited to the procedure in which it is defined. Use the keyword – to define the procedure.
- – The scope is an entire program or module. Use the keyword in the module.
- – This is for user-defined array types
Regular Arrays
The regular arrays are the usual way of declaring an array using the Dim
statement. The syntax to declare a regular array is given below:
Dim [WithEvents] variable_name [(Subscripts)] [As [New] type] [, [WithEvents] variable2_name [(Subscripts)] [As [New] type] ...
- – This is an optional parameter, and it is required when the variable is an object-variable type. An object-variable is used in modules with class or class modules.
- – It is the user-defined name of the variable.
- – They are optional parameters. An integer value subscript value indicates the number of members in an array. There are two ways to declare subscript:
[lower To] upper
Example:
TotalAmount( 10 To 20)
- – This is an optional parameter and required only when the variable is object-variable type.
- – Defines the variable type which can be one of the following: and .
Example:
Assigning Values to Regular Array Elements
To assign values to array elements refer to them using an index and then assign value like any normal variable.
For Example:
Private Sub BtnTax_Click()
Dim salary(10) As Double
Dim Location(10) As String
Location(9) = "Nottingham"
Location(7) = "France"
Print Location(9)
Print Location(7)
salary(2) = 2003
End Sub
In the next section, we will discuss declaring dynamic arrays and assigning values to them.
Dynamic Arrays
A dynamic array does not have a fixed size, it is expanded dynamically. The Dim
statement is used to declare an array without any subscript value (between the parentheses) .
For example,
Dim Accounts ( ) As Integer
Dim Employee( ) As String
The next step after declaring the array is to set the length of the array. Since this is a dynamic array we could increase the size multiple times.
The syntax:
ReDim [Preserve] variable_name (subscript) [As type]
- – To declare the size of the dynamic array.
- – This keyword is to preserve the data while expanding the size of the existing array.
- – User-defined name of the array.
- – The value of subscript can be an integer or upper.
- – This defines the data type of the array. It could be and .
Example:
Array ( ) Function in VB 6
The Array ( ) function is a special function in Vb 6 that takes array values and directly assign it to a variant type variable. The variant type variable becomes an array of the type supplied in the argument list.
Syntax:
Array(argument list)
Example:
Dim Tot As Variant
Tot = Array(23, 44, 56)
' Output the last element of the array, the size of the array is set automatically
Print Tot(2)
Summary of Statements and Functions
Here is a list of array handling statements and functions provided by VB 6.
Statements or Functions | Description |
Dim, Public, Static, ReDim, Private | Declare or Initialize an array |
Erase, ReDim | Reinitialize an array |
Array | Create an Array |
IsArray | Verify an array |
LBound, UBound | Find the upper or lower limit of an array |
Option Base | Change default lower limit 0 to a non-negative integer value |