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 –
One-dimensional array and .
Multi-dimensional arrays The VB 6 can have a maximum of 60 dimensions. The arrays in Vb 6 are also classified based on keywords used to declare them:
- Regular arrays – It is declared using keyword
Dimwhole values remain variable.
- Dynamic arrays – A standard array can be expanded for more space, hence, it is called dynamic array. You can use keyword –
ReDimto redeclare the array.
- Static arrays – 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.
- Private arrays – The scope is limited to the procedure in which it is defined. Use the keyword –
Privateto define the procedure.
- Public arrays – The scope is an entire program or module. Use the keyword
Publicin the module.
- Type arrays – This is for user-defined array types
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] ...
- WithEvents – 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.
- Variable_name – It is the user-defined name of the variable.
- Subscripts – 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
TotalAmount( 10 To 20)
- New – This is an optional parameter and required only when the variable is object-variable type.
- tpye- Defines the variable type which can be one of the following: Byte, Boolean, Currency, Long, Double, Single, String and Date.
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.
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.
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) .
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.
ReDim [Preserve] variable_name (subscript) [As type]
- ReDim – To declare the size of the dynamic array.
- Preserve – This keyword is to preserve the data while expanding the size of the existing array.
- Variable_name – User-defined name of the array.
- Subscript – The value of subscript can be an integer or [lower To] upper.
- type – This defines the data type of the array. It could be Byte, Boolean, Long, Single, Double, Currency, Date, Integer, and String.
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.
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|