Export (0) Print
Expand All

7.6 Structures

Visual Studio .NET 2003

Structures are value types that inherit from System.ValueType. Structures are similar to classes in that they represent data structures that can contain data members and function members. Unlike classes, however, structures do not require heap allocation.

In the case of classes, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With structures, the variables each have their own copy of the data, so it is not possible for operations on one to affect the other, as the following example illustrates:

Structure Point
    Public x, y As Integer

    Public Sub New(ByVal x As Integer, ByVal y As Integer)
        Me.x = x
        Me.y = y
    End Sub
End Structure 

Given the above declaration the following code fragment outputs the value 10:

Point a = new Point(10, 10)
Point b = a

a.x = 100
System.Console.WriteLine(b.x)

The assignment of a to b creates a copy of the value, and b is thus unaffected by the assignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and b would reference the same object.

StructureDeclaration ::=
   [ Attributes ] [ StructureModifier+ ] Structure Identifier
      LineTerminator
   [ TypeImplementsClause+ ]
   [ StructMemberDeclaration+ ]
   End Structure LineTerminator
StructureModifier ::= AccessModifier | Shadows

See Also

7.2 Interface Implementation | 7.6.1 Structure Members | 7.1 Value Types and Reference Types | 7.3 Primitive Types | 7.4 Enumerations | 7.5 Classes | 7.7 Standard Modules | 7.8 Interfaces | 7.9 Arrays | 7.10 Delegates | Structure Statement (Visual Basic Language Reference) | Structures: Your Own Data Types (Visual Basic Language Concepts)

Show:
© 2014 Microsoft