Structure Statement - Visual Basic (original) (raw)

Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises.

Syntax

[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Partial ] _
Structure name [ ( Of typelist ) ]
    [ Implements interfacenames ]
    [ datamemberdeclarations ]
    [ methodmemberdeclarations ]
End Structure

Parts

Term Definition
attributelist Optional. See Attribute List.
accessmodifier Optional. Can be one of the following: - Public- Protected- Friend- Private- Protected Friend- Private Protected See Access levels in Visual Basic.
Shadows Optional. See Shadows.
Partial Optional. Indicates a partial definition of the structure. See Partial.
name Required. Name of this structure. See Declared Element Names.
Of Optional. Specifies that this is a generic structure.
typelist Required if you use the Of keyword. List of type parameters for this structure. See Type List.
Implements Optional. Indicates that this structure implements the members of one or more interfaces. See Implements Statement.
interfacenames Required if you use the Implements statement. The names of the interfaces this structure implements.
datamemberdeclarations Required. Zero or more Const, Dim, Enum, or Event statements declaring data members of the structure.
methodmemberdeclarations Optional. Zero or more declarations of Function, Operator, Property, or Sub procedures, which serve as method members of the structure.
End Structure Required. Terminates the Structure definition.

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.

Structures default to Friend access. You can adjust their access levels with the access modifiers. For more information, see Access levels in Visual Basic.

Rules

Behavior

Example

The following 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. 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.

See also