Thursday 14 June 2007

Some VB.Net Keywords

  • Mustoverride
The MustOverride keyword specifies that a property or procedure in a base class must be overridden in a derived class before it can be used.

The MustOverride keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example :Public MustOverride Sub Paint(ByVal g As Integer, ByVal r As Integer)
Public MustOverride Property abc() As String The derived class must implement this property.

  • Overridable
The Overridable keyword specifies that a property or method can be overridden in a derived class.

Overridable keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example: Public Overridable Function Abc() As String
End Function

The difference between MustOverride and Overridable method lie in the implementation , methods marked with MustOverride can not have an implementation while members marked with Overridable may or may not have an implementation in the base class

  • Overrides

The Overrides keyword specifies that a property or method overrides a member inherited from a base class.

The Overrides keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example : Public Overrides Sub A(ByVal str As String)

  • Overloads
The Overloads keyword declares a property or method with the same name as an existing member, but with an argument list different from the original member.

The Overrides keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example:
'Base Class
Public MustInherit Class BaseClass
Public MustOverride Sub A()
End Class
'Derived Class
Public Class DerivedClass
Inherits BaseClass
Public Overloads Overrides Sub A()
End Sub
Public Overloads Sub A(ByVal test As String)
End Sub
End Class

  • Shadow
The Shadows keyword indicates that a declared programming element shadows, or hides, an identically named element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind.

The Shadows keyword is used in these contexts:
Class Statement
Const Statement
Declare Statement
Delegate Statement
Dim Statement
Enum Statement
Event Statement
Function Statement
Interface Statement
Property Statement
Structure Statement
Sub Statement

Example :
The Shadows and Overloads keywords cannot be specified at the same time.

Class Base
Sub F()
End Sub

Sub F(ByVal i As Integer)
End Sub

Sub G()
End Sub

Sub G(ByVal i As Integer)
End Sub
End Class

Class Derived
Inherits Base
' Only hides F(Integer)
Overloads Sub F(ByVal i As Integer)
End Sub

' Hides G() and G(Integer)
Shadows Sub G(ByVal i As Integer)
End Sub
End Class


Finally a Very Good Question :

Whats the output of the following program

Class A
Public Overridable Sub F()
Console.WriteLine("A.F")
End Sub
End Class

Class B
Inherits A
Public Overrides Sub F()
Console.WriteLine("B.F")
End Sub
End Class

Class C
Inherits B
Public Shadows Overridable Sub F()
Console.WriteLine("C.F")
End Sub
End Class

Class D
Inherits C

Public Overrides Sub F()
Console.WriteLine("D.F")
End Sub
End Class

Module Test
Sub Main()
Dim d As New D()
Dim a As A = d
Dim b As B = d
Dim c As C = d
a.F()
b.F()
c.F()
d.F()
End Sub
End Module

Answer

B.F
B.F
D.F
D.F