Compartilhar via


Variáveis de estrutura (Visual Basic)

Once you have created a structure, you can declare procedure-level and module-level variables as that type. For example, you can create a structure that records information about a computer system. The following example demonstrates this.

Public Structure systemInfo
    Public cPU As String
    Public memory As Long
    Public purchaseDate As Date
End Structure

You can now declare variables of that type. The following declaration illustrates this.

Dim mySystem, yourSystem As systemInfo
ObservaçãoObservação

In classes and modules, structures declared using the Instrução Dim (Visual Basic) default to public access. If you intend a structure to be private, make sure you declare it using the Private (Visual Basic) keyword.

Access to Structure Values

To assign and retrieve values from the elements of a structure variable, you use the same syntax as you use to set and get properties on an object. You place the member access operator (.) between the structure variable name and the element name. The following example accesses elements of the variables previously declared as type systemInfo.

mySystem.cPU = "486"
Dim tooOld As Boolean
If yourSystem.purchaseDate < #1/1/1992# Then tooOld = True

Assigning Structure Variables

You can also assign one variable to another if both are of the same structure type. This copies all the elements of one structure to the corresponding elements in the other. The following declaration illustrates this.

yourSystem = mySystem

If a structure element is a reference type, such as a String, Object, or array, the pointer to the data is copied. In the previous example, if systemInfo had included an object variable, then the preceding example would have copied the pointer from mySystem to yourSystem, and a change to the object's data through one structure would be in effect when accessed through the other structure.

Consulte também

Tarefas

Solucionando problemas de tipos de dados (Visual Basic)

Como: Declarar uma estrutura (Visual Basic)

Referência

Instrução Structure

Conceitos

Tipos de dados no Visual Basic

Tipos de dados de composição (Visual Basic)

Estruturas e outros elementos de programação (Visual Basic)

Estruturas e Classes (Visual Basic)

Outros recursos

Tipos de dados elementares (Visual Basic)

Implementação de tipos de dados (Visual Basic)

Estruturas (Visual Basic)