Home » VB 6.0 Tutorial » VB 6 Arrays

VB 6 Arrays

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:

  1. Regular arrays – It is declared using keyword Dim whole values remain variable.
  2. Dynamic arrays – A standard array can be expanded for more space, hence, it is called dynamic array. You can use keyword – ReDim to redeclare the array.
  3. 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.
  4. Private arrays – The scope is limited to the procedure in which it is defined. Use the keyword – Private to define the procedure.
  5. Public arrays – The scope is an entire program or module. Use the keyword Public in the module.
  6. Type arrays – 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] ...
  • 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

Example:

             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.

Example:

Regular Array Declarations
Regular Array Declarations

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]
  • 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.

Example:

Dynamic Array - ReDim Statement
Dynamic Array – ReDim Statement

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 FunctionsDescription
Dim, Public, Static, ReDim, PrivateDeclare or Initialize an array
Erase, ReDimReinitialize an array
ArrayCreate an Array
IsArrayVerify an array
LBound, UBoundFind the upper or lower limit of an array
Option BaseChange default lower limit 0 to a non-negative integer value