Structures handle instantiation somewhat differently from object references. When you declare a reference variable, Visual Basic does not automatically allocate the object to which the variable points. In contrast, when you declare a value type such as a structure, Visual Basic automatically allocates space for the variable’s data. That means you never need to use the New keyword to instantiate a structure.
However, the Visual Basic compiler warns you if you do not explicitly initialize a structure variable before using it. To satisfy the compiler, you can use the New keyword to initialize the variable when you declare it.
A structure can also provide constructors, and you can use those constructors to initialize the structure. The following code defines the SPerson structure and gives it a constructor that takes two parameters, the second optional:
Public Structure SPerson
Public FirstName As String
Public LastName As String
Public Sub New(
ByVal first_name As String,
Optional ByVal last_name As String = "<unknown>")
FirstName = first_name
LastName = last_name
End Sub
End Structure
To use a structure’s constructor, you initialize the structure with the New keyword much as you initialize a reference variable. The following code allocates an SPerson structure variable using the two-parameter constructor:
Dim artist As New SPerson("Sergio", "Aragones")
You can also use structure constructors later to reinitialize a variable or set its values, as shown here:
' Allocate the artist variable.
Dim artist As SPerson
' Do something with artist.
...
' Reset FirstName and LastName to Nothing.
artist = New SPerson
...
' Set FirstName and LastName to Bill Amend.
artist = New SPerson("Bill", "Amend")
As is the case with classes, you can use a With clause to set structure values when you initialize a structure variable. For example, the following code creates a new SPerson structure and sets its FirstName and LastName values:
Dim artist As New SPerson() With {.FirstName = "Anna", .LastName = "Aux"}
Structure and class constructors are very similar, but there are some major differences:
The following code demonstrates these differences. The CPerson class defines initial values for its FirstName and LastName variables, provides an empty constructor, and provides a two-parameter constructor. The SPerson structure cannot define initial values for FirstName and LastName and cannot provide an empty constructor.
' Class.
Public Class CPerson
Public FirstName As String = "<unknown>" ' Initialization value allowed.
Public LastName As String = "<unknown>" ' Initialization value allowed.
' Empty constructor allowed.
Public Sub New()
End Sub
' Two-parameter constructor allowed.
Public Sub New(first_name As String, last_name As String)
FirstName = first_name
LastName = last_name
End Sub
End Class
' Structure.
Public Structure SPerson
Public FirstName As String ' = "<unknown>" ' Initialization NOT allowed.
Public LastName As String ' = "<unknown>" ' Initialization NOT allowed.
'' Empty constructor NOT allowed.
'Public Sub New()
'End Sub
' Two-parameter constructor allowed.
Public Sub New(first_name As String, last_name As String)
FirstName = first_name
LastName = last_name
End Sub
End Structure