Hi Friends
I am little confused about the shadows keyword in VB.NET
could anyone explain with an example about Shadows keyword
The "Shadows" keyword is used for a special kind of overloading. In
its broadest terms, it is used to overload functions and offer a
different return type (for example). This is particularly useful when
implementing strongly-typed objects.
Here is an example of a strongly typed ArrayList (for "ITestClass"
objects):
--begin example--
Public Class TestClassArrayList
Inherits System.Collections.ArrayList
Implements ITestClassCollection
Public Sub New()
MyBase.New
End Sub
Public Shadows Function Add(ByVal Value As ITestClass) As
Integer Implements ITestClassCollection.Add
Return MyBase.Add(Value)
End Function
Public Shadows Function Remove(ByVal Value As ITestClass)
Implements ITestClassCollection.Remove
MyBase.Remove(Value)
End Function
Default Shadows Public Property Item(ByVal index As Integer)
As ITestClass Implements ITestClassCollection.Item
Get
Return MyBase.Item(index)
End Get
Set(ByVal Value As ITestClass)
MyBase.Item(index) = Value
End Set
End Property
Public Shadows Function BinarySearch(ByVal Index As Integer,
ByVal Count As Integer, ByVal Value As ITestClass, ByVal Comparer As
IComparer) As Integer
Return MyBase.BinarySearch(Index, Count, Value,
Comparer)
End Function
Public Shadows Function BinarySearch(ByVal Value As
ITestClass) As Integer
Return MyBase.BinarySearch(Value)
End Function
Public Shadows Function BinarySearch(ByVal Value As
ITestClass, ByVal Comparer As IComparer) As Integer
Return MyBase.BinarySearch(Value, Comparer)
End Function
Public Shadows Function Contains(ByVal item As ITestClass) As
Boolean Implements ITestClassCollection.Contains
Return MyBase.Contains(item)
End Function
Public Shadows Function IndexOf(ByVal Value As ITestClass) As
Integer Implements ITestClassCollection.IndexOf
Return MyBase.IndexOf(Value)
End Function
Public Shadows Function IndexOf(ByVal Value As ITestClass,
ByVal startIndex As Integer) As Integer
Return MyBase.IndexOf(Value, startIndex)
End Function
Public Shadows Function IndexOf(ByVal Value As ITestClass,
ByVal startIndex As Integer, ByVal count As Integer) As Integer
Return MyBase.IndexOf(Value, startIndex, count)
End Function
Public Shadows Function LastIndexOf(ByVal Value As ITestClass)
As Integer
Return MyBase.LastIndexOf(Value)
End Function
Public Shadows Function LastIndexOf(ByVal Value As ITestClass,
ByVal startIndex As Integer) As Integer
Return MyBase.LastIndexOf(Value, startIndex)
End Function
Public Shadows Function LastIndexOf(ByVal Value As ITestClass,
ByVal startIndex As Integer, ByVal count As Integer) As Integer
Return MyBase.LastIndexOf(Value, startIndex, count)
End Function
Public Shadows Sub Insert(ByVal index As Integer, ByVal value
As ITestClass)
MyBase.Insert(index, value)
End Sub
End Class
--end example--
Here's a closer look at the Add(Value) method. The base class
(ArrayList) has the method:
Add(Value As Object)
But in a strongly-typed system, our collection is a "collection of
ITestClass objects" and NOT a "collection of Objects". So, we don't
want to expose the base class's Add method - we want the parameter to
force an ITestClass object instead. The Shadows keyword has the effect
of replacing the base class method with the new one.
The TestClassArrayList therefore can only hold ITestClass objects. We
still have access to the non-type specific functions/properties such
as Count() through the inheritance.
The same can be done with Collection, HybridDictionary, ListBox etc.
and is my preferred modelling approach. You might think this is a lot
of work to achieve little, but I've written a code-generator to
produce all of these things for me. In fact, it generates a complete
DAL for SQL Server databases, as it incorporates a complete template
parser. If anyone would like to beta-test it, I'd very much appreciate
it!
Rgds,