The MyBase Keyword Nested Classes Destructors

82 • The keywords can behave differently when used in a class from which other classes are derived. Consider this code: • Public Class BaseClass • Public Sub Method1 • Console.WriteLineInvoking Me.Method2... • Me.Method2 • Console.WriteLineInvoking MyClass.Method2... • MyClass.Method2 • End Sub • Public Overridable Sub Method2 • Console.WriteLineBaseClass.Method2 • End Sub • End Class • • Public Class DerivedClass • Inherits BaseClass • Public Overrides Sub Method2 • Console.WriteLineDerivedClass.Method2 • End Sub End Class This code defines two classes: BaseClass and DerivedClass. BaseClass defines two methods: Method1 and Method2. DerivedClass inherits Method1 but provides its own implementation for Method2. Now consider the following instantiation of DerivedClass, as well as a call through it to the Method1 method: Dim d As New DerivedClass d.Method1 This produces the following output: Invoking Me.Method2... DerivedClass.Method2 Invoking MyClass.Method2... BaseClass.Method2 The call to Method1 through the DerivedClass instance calls the Method1 implementation inherited from BaseClass. Method1 calls Method2 twice: once through the Me keyword and once through the MyClass keyword. The Me keyword is a reference to the actual object instance, which is of type DerivedClass. Therefore, Me.Method2 invokes the DerivedClass classs implementation of Method2. In contrast, the MyClass keyword is used for referencing members in the class in which the code is defined, which in this case is the BaseClass class. Therefore, MyClass.Method2 invokes the BaseClass classs implementation of Method2.

2.14.9 The MyBase Keyword

The MyBase keyword is used to access methods on the base class. This feature is commonly used when an overriding method needs to call the base-class implementation of the same method: Public Class BaseClass Public Overridable Sub DoSomething ... 83 End Sub End Class Public Class DerivedClass Inherits BaseClass Public Overrides Sub DoSomething Start by calling the base-class implemenation of DoSomething. MyBase.DoSomething Then continue on with additional stuff required by DerivedClass. ... End Sub End Class

2.14.10 Nested Classes

Class definitions can be nested. The nested class is considered a member of the enclosing class. As with other members, dot notation is used for accessing the inner class definition. Consider this nested class definition: Public Class OuterClass Public Class InnerClass Public Sub SomeMethod Console.WriteLineHello from InnerClass.SomeMethod End Sub End Class End Class Instantiating an object of type InnerClass requires qualifying the name with the name of the enclosing class: Dim x As New OuterClass.InnerClass x.SomeMethod The accessibility of the inner-class declaration can be controlled with the class declarations access modifier. For example, in the following definition, InnerClass has been declared with the Private modifier, making it visible only within the confines of the OuterClass class: Public Class OuterClass Private Class InnerClass ... End Class End Class Classes can be nested as deeply as desired.

2.14.11 Destructors

Just as constructors are methods that run when objects are instantiated, it is often convenient to define methods that run when objects are destroyed that is, when the memory that was allocated to them is returned to the pool of free memory. Such a method is called a destructor. Visual Basic .NET doesnt have special syntax for declaring destructors, as it does for constructors. Instead, Visual Basic .NET uses the specially named methods Finalize and Dispose to perform the work normally associated with destructors. Because this mechanism is actually part of the .NET Framework rather than Visual Basic .NET, it is explained in Chapt er 3 , under Memory Management and Garbage Collection. 84

2.14.12 Early Versus Late Binding