Partilhar via


Como: Ceder uma matriz a outra matriz (Visual Basic)

Because arrays are objects, you can use them in assignment statements like other object types. An array variable holds a pointer to the data constituting the array elements and the rank and length information, and an assignment copies only this pointer.

To assign one array to another array

  1. Ensure that the two arrays have the same rank (number of dimensions) and compatible element data types.

  2. Use a standard assignment statement to assign the source array to the destination array. Do not follow either array name with parentheses.

    Dim formArray() As System.Windows.Forms.Form
    Dim controlArray() As System.Windows.Forms.Control
    controlArray = formArray
    

When you assign one array to another, the following rules apply:

  • Equal Ranks. The rank (number of dimensions) of the destination array must be the same as that of the source array.

    Provided the ranks of the two arrays are equal, the dimensions do not need to be equal. The number of elements in a given dimension can change during assignment.

  • Tipos de elemento. Tanto as duas matrizes devem ter tipo de referência elementos ou as duas matrizes devem ter tipo de valor elementos. For more information, see Implementação de tipos de dados (Visual Basic).

    • If both arrays have value type elements, the element data types must be exactly the same. The only exception to this is that you can assign an array of Enum elements to an array of the base type of that Enum.

    • If both arrays have reference type elements, the source element type must derive from the destination element type. When this is the case, the two arrays have the same inheritance relationship as their elements. This is called array covariance.

The compiler reports an error if the above rules are violated, for example if the data types are not compatible or the ranks are unequal. You can add error handling to your code to make sure that the arrays are compatible before attempting an assignment. You can also use the Operador TryCast (Visual Basic) keyword if you want to avoid throwing an exception.

Consulte também

Tarefas

Como: Alterar uma matriz para uma matriz diferente (Visual Basic)

Como: Passar uma matriz para um procedimento ou propriedade (Visual Basic)

Como: Retornar uma matriz de um procedimento ou propriedade (Visual Basic)

Solucionando problemas de matrizes (Visual Basic)

Referência

Declaração Enum (Visual Basic)

Conceitos

Matrizes no Visual Basic

Conversões de matriz (Visual Basic)