Overloading

 <  Day Day Up  >  

When a member in a derived class shadows a method in the base class, it hides that method in the derived class. This handles the situation where a base class is versioned, but what about the situation in which the programmer does not wish to hide the base class method? For instance, given a base class that defines a Print method, the programmer may wish to add another Print overload. However, shadowing defeats this.

 Class Base   Sub Print(ByVal s As String)   End Sub End Class Class Derived   Inherits Base   ' This hides Base.Print(String)   Shadows Sub Print(ByVal d As Decimal)   End Sub End Class Module Test   Sub Main()     Dim d As Derived = New Derived()     ' Error: Derived.Print(Decimal) hides Base.Print(Object)     d.Print("abc")   End Sub End Module 

Instead of declaring a derived method or property with the Shadows keyword, you can declare it with the Overloads keyword. This causes the derived method or property only to hide methods or properties in the base class with the same name and parameter list as the method . In the following example, the method Derived.Print no longer hides the method Base.Print .

 Class Base   Sub Print(ByVal s As String)   End Sub End Class Class Derived   Inherits Base   Overloads Sub Print(ByVal d As Decimal)   End Sub End Class Module Test   Sub Main()     Dim d As Derived = New Derived()     ' OK: Calls Base.Print(String)     d.Print("abc")   End Sub End Module 

The keyword Overloads is used because it allows methods and properties to be overloaded across the inheritance hierarchy.

Style

The Overloads keyword is required only when you are doing overloading across the inheritance hierarchy. It is never required when you are doing overloading within a class.


Only methods and properties can use the Overloads keyword, because they are the only kinds of members that can be overloaded. If a member declared as Overloads conflicts with a base class member that is not the same kind of member, Overloads is the equivalent to Shadows . In the following example, Derived.Value hides Base.Value even though it is declared as Overloads , because one is a property and the other is a method.

 Class Base   ReadOnly Property Value() As Integer     Get       ...     End Get   End Property End Class Class Derived   Inherits Base   ' This hides Base.Value   Overloads Function Value() As Integer   End Function End Class 

Also remember that a method declared as Overloads will still hide a member with the same name and parameter list in the base class. This is because it would otherwise be impossible for the compiler to choose between two methods with exactly the same name and parameter list. In the following example, the method Derived.Print will be called because it has exactly the same parameter list as the method Base.Print .

 Class Base   Sub Print(ByVal s As String)   End Sub End Class Class Derived   Inherits Base   Overloads Sub Print(ByVal s As String)   End Sub End Class Module Test   Sub Main()     Dim d As Derived = New Derived()     ' Calls Derived.Print(String)     d.Print("abc")   End Sub End Module 

As with Shadows , if one overloaded method is declared as Overloads , they all have to be. For example, if Print were overloaded in Derived , it would have to be declared as follows .

 Class Derived   Inherits Base   Overloads Sub Print()     ...   End Sub   Overloads Sub Print(ByVal s As String)     ...   End Sub End Class 
 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net