Visual Basic Concepts

Advanced Features of Arrays

See Also

Although arrays are most commonly used to store groups of variables, there are several other ways in which arrays are useful. You can assign the contents of one array to another, create functions that return arrays, and create properties that return arrays. In many cases these techniques can improve the performance of your application.

Assigning Arrays

Just as you can assign the contents of one variable to another, for example strA = strB, you can also assign the contents of one array to another. Imagine, for instance, that you wanted to copy an array of bytes from one location to another. You could do it by copying one byte at a time, like this:

Sub ByteCopy(oldCopy() As Byte, newCopy() As Byte)
   Dim i As Integer
   ReDim newCopy (Lbound(oldCopy) To UBound(oldCopy)

   For i – Lbound(oldCopy) To Ubound(oldCopy)
      newCopy(i) = oldCopy(i) 
   Next
End Sub

A much more efficient way to do this is to assign one array to another:

Sub ByteCopy(oldCopy() As Byte, newCopy() As Byte)
   newCopy = oldCopy
End Sub

With variable assignment there are certain rules that you need to keep in mind. For example, although you can assign a variable declared as Integer to a variable declared as Long without any problem, assigning a Long to an Integer could easily lead to an overflow error. In addition to data typing rules, array assignments have additional rules involving the number of dimensions, the size of those dimensions, and whether an array is fixed or dynamic.

Attempting to assign arrays with different dimensions and/or data types may or may not succeed, depending on several factors:

The following table shows the effects of these factors:

Left-hand Side Number of Dimensions Match? Number of Elements Match? Result of Assignment
Dynamic No Yes or No Succeeds. Left-hand side ReDim’s to match right-hand side if necessary.
Dynamic Yes No Succeeds. Left-hand side ReDim’s to match right-hand side if necessary.
Dynamic Yes Yes Succeeds.
Fixed Yes or No Yes or No Fails with a compilation error.

Errors can occur both at compile time and at run time (for example, if data types can’t be coerced or if an assignment attempts to ReDim a fixed size array.) As the programmer, it’s up to you to add error handling to make sure that the arrays are compatible before attempting an assignment.

Returning an Array from a Function

It’s possible for a function to return an array of values. For example, you might want to return an array of bytes from a function without having to perform conversions to and from a string.

Here’s a simple example of a function that returns an array of bytes:

Private Sub Form_Load()
   Dim b As Byte
   Dim I As Integer
   Dim ReturnArray() As Byte

   i  = Cbyte(54)
   ReturnArray() = ArrayFunction(b)
   For i = 0 To Ubound(ReturnArray)
      Debug.Print ReturnArray(i)
   Next
End Sub

Public Function ArrayFunction(b As Byte) As Byte()
   Dim x(2) As Byte

   x(0) = b
   x(1) = b + CByte(200)
   x(2) = b + b
   

   ArrayFunction = x   
End Function

After running the above example, ReturnArray() would be a three-element array containing the values assigned to the array in the ArrayFunction. Note that the array must be of the same data type as the function (in this case, Byte). Because this is a function call, you can pass the array without the parentheses.

Note   Although it’s possible to return the array by assigning another array (ArrayFunction = x()), this isn’t recommended for performance reasons.

You must specify a type for a function that returns an array; that type may be a Variant. Thus Function X() As Variant() would work whereas Function X() As () would fail.

When calling a function that returns an array, the variable to hold the return values must be an array and must be of the same data type as the function, otherwise it will display a "Type Mismatch" error.

For More Information   To learn more about using arrays, see "Arrays" in "Programming Fundamentals." For information about returning arrays from properties, see "Putting Property Procedures to Work for You" in "Programming with Objects."