Console.WriteLine("A.DoSomething")
End Sub
End Class
CHAPTER 3 n CLASSES AND STRUCTURES 39
Public Class B
Inherits A
Public Sub New()
MyBase.New(123)
'Constructor code here
End Sub
Public Overloads Overrides Sub DoSomething()
System.Console.WriteLine("B.DoSomething")
MyBase.DoSomething()
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim b As B = New B()
b.DoSomething()
End Sub
End Class
Here??™s the output from the preceding code:
B.DoSomething
A.DoSomething
In the preceding example, you can see two uses of the MyBase keyword. The first use is in
the constructor for Class B. As a derived class doesn??™t inherit instance constructors, when initializing
the object, it is sometimes necessary to explicitly call one of the base class constructors
during initialization of the derived class. This syntax is in the Class B instance constructor. The
base class initialization occurs after the declaration of the derived class constructor??™s parameter
list but before the constructor code block. The section titled ???Creating Objects??? discusses the
ordering of constructor calls and object initialization.
The second use of the MyBase keyword is in the B.DoSomething() implementation. In our
implementation of Class B, we borrow the DoSomething() implementation in Class A while
implementing B.
Pages:
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96