Structure Behavior
You can also reference other structuresany one of the fundamental value types, for startersin yours. Let's
examine how the method ClRed in the following code:
Private colorDefault As Color
Public Sub ClRed()
colorDefault = System.Drawing.Color.Red
End Sub
sets the default color to System.Drawing.Color.Red, which is itself a structure provided by the
System.Drawing namespaces.
Passing Structures to and from Methods
You can pass structures as arguments to method parameters ByValue and ByReference, just as you can with
any value type. This is critical, especially for returning arguments from methods. While you can pass several
arguments to the formal parameter list of a method, you cannot return more than one value type or built−in
data type.
There are many situations in which you can return a value type that sends more than a single value as a single
valuenotwithstanding the oft−cited rule and condition that you can and should only return a single value from
a method.
Structures Can Reference Objects
Structures can reference objects, even collection objects like arrays. The upcoming example includes objects
and other structures:
Public Structure Target
Private targetColor As TGridColors 'target colors
Private targetPosition As TGridCoordinates 'x,y positions on the grid
Private targetSpeed As TSpol 'significant percentage of lightspeed
Private targetType As TCraft 'the type of craft
Private targetDistance As TDistance 'distance from our ship
Private targetVector As TVector 'is the target going or coming
Private targetHistory As History
End Structure
Structure Constructors
You cannot initialize a structure's members in the declaration, but you can initialize its variables in the
constructor. The following code, from the earlier GridColor example, initializes the colorDefault variable of
System.Drawing.Color structure in the New constructor.
Private colorDefault As Color
Public Sub New(ByVal red As Integer, ByVal green As Integer, _
ByVal blue As Integer)
colorDefault = Color.FromArgb(red, green, blue)
End Sub
You are not required to provide a constructor for a struct, as you would be to create an object instance. Even if
you provide the New method, you do not need to call New in the declaration. Please note: if you neither
provide nor call a constructor, zero will be the default for the struct's fields. This also explains why you cannot
247