Share via


How to: Change an Array to a Different Array (Visual Basic)

It is important to distinguish between an array object and an array variable. An array variable holds a pointer to an array object, which holds the array elements and the rank and length information.

  • Once you create an array object, you cannot change its rank (number of dimensions), its dimension lengths, or the data type of its elements. You can change only the contents of its elements.

  • Once you declare an array variable, you cannot change its rank or its element data type. However, you can assign a succession of different array objects to the variable during its lifetime. These array objects can have different dimension lengths.

To change an array variable to point to a different array object

  • Use a standard assignment statement to assign the source array to the destination array.

    Dim array1(4), array2(19) As String
    array2 = array1
    

You can change an array variable to point to an array object with different dimension lengths, but you cannot change it to point to an array object of a different data type. This means that the rank and the element data type must remain the same, because they are inherently part of the array variable's data type.

See Also

Tasks

How to: Initialize Array Variables in Visual Basic

How to: Assign One Array to Another Array (Visual Basic)

How to: Pass an Array to a Procedure or Property (Visual Basic)

How to: Return an Array from a Procedure or Property (Visual Basic)

Troubleshooting Arrays (Visual Basic)

Other Resources

Arrays in Visual Basic