Structures (Value Types) in VB.NET
On this page:
Introduction
In VB.NET, a structure (often referred to as a Structure
or User-Defined Type (UDT)
) is a value type that can encapsulate related data and functionality. Unlike classes, which are reference types, structures are copied when passed as arguments to methods or assigned to other variables. This makes them suitable for representing small, lightweight data entities.
Defining Structures
You define a structure using the Structure
and End Structure
keywords. A structure can contain variables (fields), constants, properties, methods, and events.
Public Structure Point
Public X As Integer
Public Y As Integer
Public Sub Move(ByVal dx As Integer, ByVal dy As Integer)
X += dx
Y += dy
End Sub
Public ReadOnly Property Magnitude() As Double
Get
Return Math.Sqrt(X * X + Y * Y)
End Get
End Property
End Structure
Structure Members
Structures can contain the following types of members:
- Fields: Variables to hold data.
- Properties: Define how to access and modify the structure's data.
- Methods: Subroutines or functions that perform operations.
- Constructors: Special methods to initialize the structure.
- Constants: Values that are fixed at compile time.
- Events: Notifications raised by the structure.
By default, structure members are Public
. You can explicitly specify access levels like Private
, Protected
, etc.
Creating Structure Instances
You can create an instance of a structure using the New
keyword or by simply declaring a variable of the structure type. If you use New
, the default constructor is called. If you declare a variable without New
, the fields are initialized to their default values (0 for numeric types, Nothing
for object types, etc.).
' Using New keyword (calls default constructor)
Dim p1 As New Point()
p1.X = 10
p1.Y = 20
' Declaring a variable (fields initialized to defaults)
Dim p2 As Point
p2.X = 5
p2.Y = 15
' Calling a method
p1.Move(5, 5) ' p1.X is now 15, p1.Y is now 25
' Accessing a property
Console.WriteLine($"Magnitude of p1: {p1.Magnitude}")
Structures vs. Classes
Here's a comparison of structures and classes:
Feature | Structure | Class |
---|---|---|
Type | Value Type | Reference Type |
Memory Allocation | Stack (usually) | Heap |
Assignment/Passing | Copies the entire value | Copies the reference |
Inheritance | Cannot inherit from other structures or classes (can implement interfaces) | Can inherit from one base class and implement multiple interfaces |
Nullability | Cannot be null (unless nullable type: Nullable(Of T) or T? ) |
Can be null |
Default Constructor | Implicit default constructor (cannot be explicitly defined or omitted if other constructors exist) | Implicit parameterless constructor if no other constructors are defined |
Common Uses
Structures are ideal for:
- Representing small, immutable data aggregates like coordinates (
Point
), colors (Color
), or date/time values. - Improving performance by reducing heap allocations and garbage collection overhead for frequently created small objects.
- Creating custom types that behave like built-in value types.
Choosing between a structure and a class depends on the intended use case. For simple data containers where copying is acceptable and performance is a concern, structures are often preferred. For more complex objects with identity, inheritance, and reference semantics, classes are the better choice.