By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

Help on Shadows

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* "Satish" <kv********@hotmail.com> scripsit:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
On Sat, 18 Oct 2003 00:01:11 +0530, "Satish" <kv********@hotmail.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 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,

Nov 20 '05 #3

P: n/a
"Satish" <kv********@hotmail.com> schrieb
Hi Friends
I am little confused about the shadows keyword in VB.NET
could anyone explain with an example about Shadows keyword

Have you already read the following topic?

<F1>
VS.Net
VB and VC#
Reference
Visual Basic language
Tour through VB
Feature
Declared elements
Declared element reference
Shadowing

Direct link:

http://msdn.microsoft.com/library/en...nShadowing.asp

--
Armin

Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
On Sun, 19 Oct 2003 08:29:24 +0530, "Satish" <ne******@hotmail.com>
wrote:
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.

Nov 20 '05 #6

P: n/a
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
"_Andy_" <wi******@nospamthanks.gov> wrote in message
news:gr********************************@4ax.com...
On Sun, 19 Oct 2003 08:29:24 +0530, "Satish" <ne******@hotmail.com>
wrote:
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 abase 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.

Nov 20 '05 #7

P: n/a
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.

--------------------
From: "Satish" <kv********@hotmail.com>
Subject: Help on Shadows
Date: Sat, 18 Oct 2003 00:01:11 +0530
Lines: 11
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <Oe**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 202.63.102.117
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:148090
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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



Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.