Visual Basic Language Reference  

Structure Statement

Used at module or class level to declare a structure and define the characteristics of its members.

<attrlist> ] [{ Public | Protected | Friend | Protected Friend | Private }] [ Shadows ] 
Structure name
   [ Implements interfacenames ]
   variabledeclarations
   [ proceduredeclarations ]
End Structure

Parts

attrlist
Optional. List of attributes that apply to this structure. Multiple attributes are separated by commas.
Public
Optional. Structures declared with the Public keyword have public access. There are no restrictions on the accessibility of public structures.
Protected
Optional. Structures declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Structures declared with the Friend keyword have friend access. They are accessible from within the program that contains their declaration and from anywhere else in the same assembly.
Protected Friend
Optional. Structures declared with the Protected Friend modifiers have the union of protected and friend access. They can be used by code in the same assembly, as well as by code in derived classes. Protected friend access can be specified only on members of classes.
Private
Optional. Structures declared with the Private modifier have private access. They are accessible only from within their declaration context, including from members of any nested types such as procedures.
Shadows
Optional. Indicates that this structure shadows an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable in the derived class that shadows it.
name
Required. Name of the structure. Must be a valid Visual Basic identifier.
Implements
Optional. Indicates that this structure implements the members of one or more interfaces.
interfacenames
Required if the Implements statement is used. The names of the interfaces implemented by this structure. If you use the Implements statement, it must immediately follow the Structure statement, and you must implement every member defined by every interface you specify.
variabledeclarations
Required. One or more Dim, Event, Friend, Private, or Public statements declaring variables and events that serve as data members of the structure. These declarations follow the same rules as they do outside of a structure.

You can also define constants and properties in the structure, but you must declare at least one nonshared variable or event.

proceduredeclarations
Optional. Zero or more declarations of Function, Property, or Sub procedures that serve as method members of the structure. These declarations follow the same rules as they do outside of a structure.

Each attribute in the attrlist part has the following syntax and parts:

attrname [({ attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Remarks

The Structure statement can appear only at module, namespace, or file level. This means you can declare structures in a source file or inside a module, interface, or class, but not inside a procedure. You can also define one structure inside another, but you cannot access its members through the outer structure. Instead, you must declare a variable of the inner structure's data type.

Structures can be accessed from anywhere within the module or class in which they are declared. A structure is Friend by default. To specify the accessibility in more detail, include Public, Protected, Friend, Protected Friend, or Private in the Structure statement.

You must declare every data member of a structure. This means every statement in the variabledeclarations part must use Dim, Friend, Private, or Public. If Option Strict is On, you must also include the As clause in every statement. Members declared with Dim default to Public access, and members declared without the As clause default to the Object data type.

You must define at least one nonshared variable or event in a structure. You cannot have only constants, properties, and procedures, even if some of them are nonshared.

The scope of every structure member is the entire structure.

You cannot initialize the value of any data member of a structure as part of its declaration. You must either initialize 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.

Structures support many of the same features as classes. For example, structures can have properties and methods, 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.

Example

This example uses the Structure statement to define a set of related data for an employee. It shows the use of public, friend, and private members to reflect the sensitivity of the data items.

Public Structure Employee
   ' Public members, accessible throughout declaration region.
   Public FirstName As String
   Public MiddleName As String
   Public LastName As String
   ' Friend members, accessible anywhere within the same assembly.
   Friend EmployeeNumber As Integer
   Friend BusinessPhone As Long
   ' Private members, accessible only within the structure itself.
   Private HomePhone As Long
   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
End Structure

See Also

Dim Statement | Implements Statement | Structures and Classes