Rediģēt

Kopīgot, izmantojot


Structure Variables (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

Note

In classes and modules, structures declared using the Dim Statement default to public access. If you intend a structure to be private, make sure you declare it using the Private 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.

See also