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
You can also define constants and properties in the structure, but you must declare at least one nonshared variable or event.
Each attribute in the attrlist part has the following syntax and parts:
attrname [({ attrargs | attrinit })]
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.
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.
PublicStructure
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 SubEnd Structure
Dim Statement | Implements Statement |