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

Sunday 10 June, 2007

CTS

Q. What is Common Type System (CTS)

A.The common type system defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime's support for cross-language integration. The common type system performs the following functions:

* Establishes a framework that helps enable cross-language integration, type safety, and high performance code execution.
* Provides an object-oriented model that supports the complete implementation of many programming languages.
* Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other.

The CTS provides every language running on the .NET platform with a base set of data types. While the CTS is responsible for defining the types that can be used across the .NET languages, most languages choose to implement aliases to those types. For example, a four-byte integer value is represented by the CTS type System.Int32. C# defines an alias for this called type called int.