Export (0) Print
Expand All

Structure Variables

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, as in the following code:

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, as follows.

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. For example, using the variables previously declared as SystemInfo, you can access their elements as follows.

MySystem.CPU = "486"
Dim TooOld As Boolean
If YourSystem.PurchaseDate < #1/1/1992# Then TooOld = True

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, as in the following example:

YourSystem = MySystem

If a structure element is a reference type, such as a String, Object, or array, the pointer to the data is copied. 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

Structures: Your Own Data Types | Structures and Other Programming Elements | Structures and Classes

Structure Statement

© 2014 Microsoft