Prev | Current Page 99 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

Even though instance constructors in structure value
types cannot use the MyBase keyword to call base class constructors, they can have an initializer.
It is valid for the initializer to use the Me keyword to call other constructors on the same
structure during initialization, as in the following example:
Public Structure ComplexNumber
Private Real As Double
Private Imaginary As Double
Public Sub New(ByVal Real As Double, ByVal Imaginary As Double)
Me.Real = Real
Me.Imaginary = Imaginary
End Sub
CHAPTER 3 n CLASSES AND STRUCTURES 50
Public Sub New(ByVal Real As Double)
Me.New(Real, 0)
End Sub
End Structure
Public Class EntryPoint
Shared Sub Main()
Dim valA As ComplexNumber = New ComplexNumber(1)
End Sub
End Class
The previous code introduces an initializer that calls the first constructor from the second
one, which only assigns the Real value. When an instance constructor contains an initializer,
the Me keyword behaves as a ByRef parameter in that constructor??™s body. And, since it is a ByRef
parameter, the compiler can assume that the value has been initialized properly before entry
into the method??™s code block. In essence, the initialization burden is deferred to the first constructor
whose duty it is to make sure it initializes all fields of the value.


Pages:
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
pozycjonowanie szkolenia bhp warszawa katalog stron pozycjonowanie boiler service london