On Sat, 18 Oct 2003 00:01:11 +0530, "Satish" <kv********@hot mail.com>
wrote:
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 TestClassArrayL ist
Inherits System.Collecti ons.ArrayList
Implements ITestClassColle ction
Public Sub New()
MyBase.New
End Sub
Public Shadows Function Add(ByVal Value As ITestClass) As
Integer Implements ITestClassColle ction.Add
Return MyBase.Add(Valu e)
End Function
Public Shadows Function Remove(ByVal Value As ITestClass)
Implements ITestClassColle ction.Remove
MyBase.Remove(V alue)
End Function
Default Shadows Public Property Item(ByVal index As Integer)
As ITestClass Implements ITestClassColle ction.Item
Get
Return MyBase.Item(ind ex)
End Get
Set(ByVal Value As ITestClass)
MyBase.Item(ind ex) = Value
End Set
End Property
Public Shadows Function BinarySearch(By Val Index As Integer,
ByVal Count As Integer, ByVal Value As ITestClass, ByVal Comparer As
IComparer) As Integer
Return MyBase.BinarySe arch(Index, Count, Value,
Comparer)
End Function
Public Shadows Function BinarySearch(By Val Value As
ITestClass) As Integer
Return MyBase.BinarySe arch(Value)
End Function
Public Shadows Function BinarySearch(By Val Value As
ITestClass, ByVal Comparer As IComparer) As Integer
Return MyBase.BinarySe arch(Value, Comparer)
End Function
Public Shadows Function Contains(ByVal item As ITestClass) As
Boolean Implements ITestClassColle ction.Contains
Return MyBase.Contains (item)
End Function
Public Shadows Function IndexOf(ByVal Value As ITestClass) As
Integer Implements ITestClassColle ction.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(ByV al Value As ITestClass)
As Integer
Return MyBase.LastInde xOf(Value)
End Function
Public Shadows Function LastIndexOf(ByV al Value As ITestClass,
ByVal startIndex As Integer) As Integer
Return MyBase.LastInde xOf(Value, startIndex)
End Function
Public Shadows Function LastIndexOf(ByV al Value As ITestClass,
ByVal startIndex As Integer, ByVal count As Integer) As Integer
Return MyBase.LastInde xOf(Value, startIndex, count)
End Function
Public Shadows Sub Insert(ByVal index As Integer, ByVal value
As ITestClass)
MyBase.Insert(i ndex, 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 TestClassArrayL ist 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, HybridDictionar y, 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,