Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises.
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Partial ] _ Structure name [ ( Of typelist ) ] [ Implements interfacenames ] [ datamemberdeclarations ] [ methodmemberdeclarations ] End Structure
||Optional. See Attribute List.|
||Optional. Can be one of the following:
- Protected Friend
- Private Protected
See Access levels in Visual Basic.
||Optional. See Shadows.|
||Optional. Indicates a partial definition of the structure. See Partial.|
||Required. Name of this structure. See Declared Element Names.|
||Optional. Specifies that this is a generic structure.|
||Required if you use the Of keyword. List of type parameters for this structure. See Type List.|
||Optional. Indicates that this structure implements the members of one or more interfaces. See Implements Statement.|
||Required if you use the
||Required. Zero or more
||Optional. Zero or more declarations of
||Required. Terminates the
Structure statement defines a composite value type that you can customize. A structure is a generalization of the user-defined type (UDT) of previous versions of Visual Basic. For more information, see Structures.
Structures support many of the same features as classes. For example, structures can have properties and procedures, they can implement interfaces, and they can have parameterized constructors. However, there are significant differences between structures and classes in areas such as inheritance, declarations, and usage. Also, classes are reference types and structures are value types. For more information, see Structures and Classes.
You can use
Structure only at namespace or module level. This means the declaration context for a structure must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure or block. For more information, see Declaration Contexts and Default Access Levels.
Nesting. You can define one structure within another. The outer structure is called the containing structure, and the inner structure is called a nested structure. However, you cannot access a nested structure's members through the containing structure. Instead, you must declare a variable of the nested structure's data type.
Member Declaration. You must declare every member of a structure. A structure member cannot be Protected or
Protected Friendbecause nothing can inherit from a structure. The structure itself, however, can be
You can declare zero or more nonshared variables or nonshared, noncustom events in a structure. You cannot have only constants, properties, and procedures, even if some of them are nonshared.
Initialization. You cannot initialize the value of any nonshared data member of a structure as part of its declaration. You must either initialize such a data member by means of a parameterized constructor on the structure, or assign a value to the member after you have created an instance of the structure.
Inheritance. A structure cannot inherit from any type other than ValueType, from which all structures inherit. In particular, one structure cannot inherit from another.
Implementation. If the structure uses the Implements Statement, you must implement every member defined by every interface you specify in
Access Level. Within a structure, you can declare each member with its own access level. All structure members default to Public access. Note that if the structure itself has a more restricted access level, this automatically restricts access to its members, even if you adjust their access levels with the access modifiers.
Scope. A structure is in scope throughout its containing namespace, class, structure, or module.
The scope of every structure member is the entire structure.
Lifetime. A structure does not itself have a lifetime. Rather, each instance of that structure has a lifetime independent of all other instances.
The lifetime of an instance begins when it is created by a New Operator clause. It ends when the lifetime of the variable that holds it ends.
You cannot extend the lifetime of a structure instance. An approximation to static structure functionality is provided by a module. For more information, see Module Statement.
Structure members have lifetimes depending on how and where they are declared. For more information, see "Lifetime" in Class Statement.
Qualification. Code outside a structure must qualify a member's name with the name of that structure.
If code inside a nested structure makes an unqualified reference to a programming element, Visual Basic searches for the element first in the nested structure, then in its containing structure, and so on out to the outermost containing element. For more information, see References to Declared Elements.
Memory Consumption. As with all composite data types, you cannot safely calculate the total memory consumption of a structure by adding together the nominal storage allocations of its members. Furthermore, you cannot safely assume that the order of storage in memory is the same as your order of declaration. If you need to control the storage layout of a structure, you can apply the StructLayoutAttribute attribute to the
The following example uses the
Structure statement to define a set of related data for an employee. It shows the use of
Private members to reflect the sensitivity of the data items. It also shows procedure, property, and event members.
Public Structure employee ' Public members, accessible from throughout declaration region. Public firstName As String Public middleName As String Public lastName As String ' Friend members, accessible from anywhere within the same assembly. Friend employeeNumber As Integer Friend workPhone As Long ' Private members, accessible only from within the structure itself. Private homePhone As Long Private level As Integer Private salary As Double Private bonus As Double ' Procedure member, which can access structure's private members. Friend Sub CalculateBonus(ByVal rate As Single) bonus = salary * CDbl(rate) End Sub ' Property member to return employee's eligibility. Friend ReadOnly Property Eligible() As Boolean Get Return level >= 25 End Get End Property ' Event member, raised when business phone number has changed. Public Event ChangedWorkPhone(ByVal newPhone As Long) End Structure
For more information on how to use
Structures, see Structure Variable.