STRUCTURE INSTANTIATION DETAILS

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"}

NEW NEEDED
Although you can create a structure without using the New keyword, you cannot include a With clause unless you use New.

Structure and class constructors are very similar, but there are some major differences:

  • A structure cannot declare a constructor that takes no parameters.
  • A structure cannot provide a constructor with all optional parameters, because that would allow the program to call it with no parameters.
  • Visual Basic always allows the program to use a default parameterless constructor to declare a structure variable, but you cannot make it use your parameterless constructor. Unfortunately, that means you cannot use a default constructor to guarantee that the program always initializes the structure’s values as you can with a class. If you need that feature, you should use a class instead of a structure.
  • You also cannot provide initialization values for variables declared within a structure as you can with a class. That means you cannot use this technique to provide default values for the structure’s variables.

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.57.251