Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Structure Declaration

You begin a structure declaration with the Structure statement, and you end it with the End Structure statement. Between these two statements you must declare at least one member. The members can be of any data type, but you must include at least one nonshared variable or an event.

If you want to keep an employee's name, telephone extension, and salary together in a single variable, you can declare a structure for this information as follows:

Private Structure Employee
   Public GivenName As String   ' This employee's given name.
   Public FamilyName As String   ' This employee's family name.
   Public Extension As Long   ' This employee's telephone extension.
   Private Salary As Decimal   ' This employee's annual salary.
   Public Sub GiveRaise(Raise As Double)   ' Raise this employee's salary.
      Salary *= Raise
   End Sub
   Public Event ReviewTime()   ' This employee must be reviewed.
End Structure 

If you declare a structure as Private, as in the preceding example, it is inaccessible from outside an object created from the class defining the structure. The Salary field in the preceding example is also Private, which means it is inaccessible outside the structure, even from a class that can access the structure. However, the GiveRaise() procedure is Public, so it can be called from outside.

You can also define Function procedures, properties, and events in a structure. You can define one property as the default property, provided it takes at least one argument. You can handle an event with a Shared Sub procedure.

You can specify the accessibility of a structure using the Public, Protected, Friend, or Private keyword, or you can let it default to Public. You must declare every member and specify an accessibility for it. If you use the Dim statement without any keywords, the accessibility defaults to Public.

Note   You cannot initialize any of the structure members in the structure declaration. When you declare a variable to be of a structure type, you assign values to the members by accessing them through the variable.

See Also

Structure Statement | User-Defined Data Type

Show:
© 2015 Microsoft