Structure...End Structure Statement | |
Syntax [accessModifier] [Shadows] Structure name [(Of typeParamName)] [Implements interfaceName[, interfaceName...]] statements End Structure
accessModifier (optional) Specifies the scope and accessibility of the structure. One of the following access levels: Access level | Description |
---|
Public | The structure is publicly accessible anywhere, both inside and outside of the project. | Private | The structure is accessible within the type in which it is defined. | Protected | The structure is accessible only to the type in which it is defined and to derived instances of that type. | Friend | The structure is accessible only within the project that contains the structure definition. | Protected Friend | Combines the access features of Protected and Friend. |
If omitted, the Friend access level is used.
Shadows (optional) Indicates that the structure shadows an identically named element in a base class.
name (required) The name of the structure.
typeParamName (optional; any) Adds type parameter placeholders that will later enforce strong typing when the structure is used. The Of clause implements generics, which are fully described in Chapter 10. If generics will not be used, this clause can be excluded.
interfaceName (optional) Indicates that the structure implements the members of one or more interfaces.
statements (required) Code that defines the members of the structure. Structures must contain at least one instance member. Description The Structure...End Structure statement is used to declare structures, also known as user-defined types. Structures are similar to classes, but they are value types rather than reference types. Although you can create an instance of a structure, you cannot derive another structure or class from it. Usage at a Glance The members of a structure can be fields, properties, methods, events, or types. Each member must be declared with an access modifier. You cannot assign a structure member an initial value as part of its declaration. The initial values of structure members can be set in the structure's constructor. If a structure member is an array, it cannot be explicitly dimensioned in its definition. Although structures are similar to classes, they cannot explicitly inherit, nor can they be inherited. All constructors for a structure must be parameterized, and structures cannot define destructors. Example The simplest and most common use of structures is to encapsulate related variables, as in this sample code: Structure Person Public Name As String Public Address As String Public City As String Public State As String Public Zip As String Public Age As Short End Structure An instance of Person is declared as: Dim personRecord As Person As with classes, structure members use the "dot" syntax: personRecord.Name = "Beethoven" Version Differences The Structure...EndStructure construct is new to VB under .NET. It replaces the Type...EndType construct in VB 6. The syntax and functionality differences between the two are significant. VB 6 user-defined types are groupings of basic data types. .NET structures are objects, similar to classes, with similar OOP features. Visual Basic 2005 adds support for generics to structures, as discussed in Chapter 10. See Also Class...End Class Statement |