Help on Shadows

  • Thread starter Thread starter Satish
  • Start date Start date
S

Satish

Hi Friends
I am little confused about the shadows keyword in VB.NET
could anyone explain with an example about Shadows keyword

Many thanks
Satish
 
* "Satish said:
I am little confused about the shadows keyword in VB.NET
could anyone explain with an example about Shadows keyword

Your system date/time is wrong.
 
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,
 
thanks Mr.Andy for you detailed explanation.

i have a doubt.
Shadows is not just used for method overloading, we can shadow a simple
variable of base class with the method(really a method not variable) of
derived class, for that matter any type can be shadowed by any other type.
only thing is that names should be same. This is the point where i really
got confused. Is there any similar mechanism in other OO languages

The following paragraph i am picking from .NET SDK

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

Can you give your further explanation on this,
Many thanks
 
Thanks again Mr.Andy,
you made very clear with your detailed explanation.
with your example i would able to get a new dimension in my work.
Thanks a lot,hope your extend your support future...
Satish
 
thanks Mr.Andy for you detailed explanation.

i have a doubt.
Shadows is not just used for method overloading, we can shadow a simple
variable of base class with the method(really a method not variable) of
derived class, for that matter any type can be shadowed by any other type.
only thing is that names should be same.

True. But such horridness you should really whisper.

If you start changing properties into functions, functions into
subroutines, objects into methods (etc) you will encounter problems
when using those objects from outside of the project. Although
possible, it is such an extraordinarily bad practice that I would
murder anyone who implemented such a messy construct. It is there for
completeness and the only time I ever allow the use of it is when
implementing unit-test objects. But even then...

--begin horrid example--

Public Class TestBase
Protected Collection As New ArrayList()

Public Sub WriteType()
Debug.WriteLine(Collection.GetType)
End Sub
End Class

Public Class Test
Inherits TestBase

Public Shadows Function Collection() As
Specialized.StringDictionary
Return New Specialized.StringDictionary()
End Function

Public Sub WriteLocalType()
Debug.WriteLine(Collection.GetType)
End Sub
End Class
--end horrid example--
This is the point where i really
got confused. Is there any similar mechanism in other OO languages

No, not in C++ or Java, as far as I remember. When used properly, it
is extremely useful.
The following paragraph i am picking from .NET SDK

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

Can you give your further explanation on this,

Exactly as we've said: You can shadow any property, member, function
or subroutine with anything else. The name of the element is the same,
but the type (inc. parameter declarator) will differ.
 
Just wanted to confuse people further :)

the main idea behind the shadows keyword is not to allow you to hide
elements in your base class - it's to prevent against your class being
broken by someone adding a new element to your base class that ends up
having the same name as an element you your class (for example, if you
upgrade a library version, and it now has a new element that conflicts with
an element in you class - in this case we will shadow the base class's
element and your class should still work as you expect it.)

While some people may devise other uses for the feature, this was the
primary reason to have it - using it to cause other people/code to
reference one object rathen than another can lead to those people's code
having unexpected behavior, and is generally a bad idea.

--------------------
 
Back
Top