Implementing interface methods Overriding inherited methods

74 sum += n count += 1 Next Return sum count End Function This method declares only a single parameter—an array of Integers. However, it includes the ParamArray keyword in the declaration, which tells the compiler to allow calls such as this: Compute the average of some numbers. Dim d As Double = Avg31, 41, 59, 26, 53, 58 Its worth noting that an actual array can be passed through the ParamArray parameter—something that wasnt possible in Visual Basic 6. For example: Compute the average of some numbers. Dim args As Integer = {31, 41, 59, 26, 53, 58} Dim d As Double = Avgargs

2.14.6.4 Main method

When an executable application is compiled, some code must be identified as the startup routine. This portion is what is executed when the application is run. The Visual Basic .NET compiler looks for a method named Main to fulfill this need. In .NET, all code exists as methods within classes, even the Main method. To make it accessible without having to instantiate a class, the Main method must be declared as shared. For example: Public Class App Public Shared Sub Main ... End Sub End Class The name of the class is not important. At compile time, the Visual Basic .NET compiler looks for a public shared subroutine named Main somewhere in the code. If more than one class has such a method, the developer must specify which one to use by setting the startup object in the Project Properties dialog box. If youre using the command-line compiler, specify the desired startup object with the main:class switch. A programs Main method can also appear within a Visual Basic .NET module not to be confused with .NET modules, which are described in Chapt er 3 . Because Visual Basic .NET modules are classes wherein everything is shared, the Shared keyword is not used in such a declaration: Module App Public Sub Main ... End Sub End Module

2.14.6.5 Implementing interface methods

Classes can be declared as implementing one or more interfaces. See Sect ion 2.15 . To implement an interface, the class must expose methods that correspond to the methods defined by the interface. This is done by declaring the methods in the usual way, but with an Implements clause as part of the declaration. Note the Implements keyword added to the declaration of the CompareTo method in this example: 75 Public Class SomeClass Implements IComparable Public Function CompareTo _ ByVal obj As Object _ As Integer Implements IComparable.CompareTo ... End Function End Class When appearing in a method declaration, the Implements keyword must be followed by the name of the interface and method that the given method implements. The classs method must have the same signature and return type as the interfaces method, but they need not have the same name.

2.14.6.6 Overriding inherited methods

Ex am ple 2- 7 showed how a base class can be written such that it forces derived classes to implement certain methods. In this case, the Shape class contains this declaration: Public MustOverride Sub Draw This declares the Draw method, which takes no arguments. The MustOverride keyword specifies that the base class does not provide an implementation for this method and that derived classes must do so. It is sometimes preferable to allow the base class to provide a default implementation, yet allow derived classes to substitute their own implementations. Classes that dont provide their own implementations use the base classs implementation by default. Consider the following class definitions: Class BaseClass Public Overridable Sub SomeMethod Console.WriteLineBaseClass definition End Sub End Class BaseClass Class DerivedClass Inherits BaseClass Public Overrides Sub SomeMethod Console.WriteLineDerivedClass definition End Sub End Class DerivedClass Class DerivedClass2 Inherits BaseClass End Class DerivedClass2 The BaseClass class defines a method called SomeMethod. In addition to providing an implementation of this method, the declaration specifies the Overridable keyword. This signals to the compiler that its okay to override the method in derived classes. Without this modifier, derived classes cannot override the method. The DerivedClass class overrides this method by defining a method having the same name and signature and by specifying the Overrides keyword. The Visual Basic .NET compiler requires that the Overrides keyword be present to ensure that the developer actually meant to override a base-class method. The DerivedClass2 class does not override the SomeMethod method. Calls to SomeMethod through objects of type DerivedClass2 will invoke the BaseClass definition of SomeMethod. Here is an example: 76 Dim b As New BaseClass Dim d As New DerivedClass Dim d2 As New DerivedClass2 b.SomeMethod d.SomeMethod d2.SomeMethod This code results in the following output: BaseClass definition DerivedClass definition BaseClass definition The SomeMethod implementation in the DerivedClass class can itself be overridden by a class deriving from DerivedClass. This can be prevented, if desired, by specifying the NotOverridable keyword in the definition of the SomeMethod method of the DerivedClass class, as shown here: Class DerivedClass Inherits BaseClass Public NotOverridable Overrides Sub SomeMethod ... End Sub End Class DerivedClass

2.14.6.7 Overloading