473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Compiler confusion with using Shadows keyword

For anyone who is just getting into VB.NET and/or is starting to work
with inheritance I would like to point out a potential pitfall. We
found this confusion recently when code-reviewing an application. If
you have not used the keyword 'Overridable' then read on for sure...

If you setup a child class and you want to override a method in your
base class, you may see the squiggles under your child's method name.
The pop-up/build error says something like:
C:\Projects\TestShadows\Ball.vb(28): function 'GetMessage' conflicts
with function 'GetMessage' in the base class 'BaseClass' and so should
be declared 'Shadows'.

To fix this, you may go ahead and put the 'Shadows' keyword into your
function declaration and, voila, it works. You run your project and
the overriden (so you think) method fires perfectly.

Unfortunately, this 'Shadows' keyword does not truly provide
inheritance (in my opinion). If you decide to get into polymorphism
then you'll quickly see the problem. If you have an instance of your
child class stored in a datatype defined as your base class and then
call the 'Shadowed' method, then the instance will actually call the
base class' method and not your 'Shadowed' method. I would hazard a
guess that if you are just starting out then this is not the
functionality you would expect. I would assume that you'd expect the
'Shadowed' method. I am not sure what other code situations would
highlight this discrepancy; maybe this is the only one so if you never
plan to do it then you'll never have a problem. But I've learned never
say never...

To fix this, the proper way to setup methods for inheritance is to
setup the base class' method with the keyword 'Overridable' and then
setup the child's method as 'Overrides'.

An explanation of 'Shadowing' is in the VS2003 help:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2004APR.1033/vbcn7/html/vbconShadowing.htm.
If you can't find this file then click on F1 while your cursor is over
the Shadow keyword. Once help is up, scroll down and click the
'Shadowing' link under See Also. Even though it gives an in-depth
discussion on the concept it reads like it was written by a lawyer.
Only until I wrote up some demo code was I able to see the
ramifications.

I guess there are situations where Shadows may be of use but I think
it just confuses the situation (although again, never say never). The
only example I can think of is if you were upgrading the base class
and wanted to protect the child class' interface. But then you'd
probably have a good idea of what implications Shadowing has and your
object-oriented design would key in on that. Although I'm no OO
expert, I have no idea where Shadowing fits into the design anyway. I
would think designing Interfaces would come into play way long before
Shadowing.

Bottom line is I just wanted to point out that you may be incorrectly
using Shadows where you should be using Overridable/Overrides. I think
the compiler should have been designed to suggest using
Overridable/Overrides before ever suggesting Shadows.

Following is a code sample I used to demo the issue:

1) Create a web or win project, my example is a web project.
2) Add a class file to the project.
3) Make sure the new class file is empty and paste this content:
Public Class BaseClass
Public Overridable Function GetMessage() As String
Return "In BaseClass."
End Function
End Class

Public Class ShadowsMethod
Inherits BaseClass

Public Shadows Function GetMessage() As String
Return "In ShadowingMethodClass."
End Function
End Class

Public Class OverridesMethod
Inherits BaseClass

Public Overrides Function GetMessage() As String
Return "In OverridesMethod."
End Function
End Class

4) This code is for the page_load of a web form. If you want to use a
winform, you'll have to modify the Response.Writes to methods that
write to something on a winform (textbox, listbox, label, etc.)
Dim oInherits As New OverridesMethod
Dim oShadows As New ShadowsMethod
Dim oBase As BaseClass

'Call child's GetMessage
Response.Write(oShadows.GetMessage)

'Assign Child instance to a base datatype
oBase = oShadows

Response.Write("<br>Call method on derived class stored in
BaseClass datatype: ")
Response.Write(oBase.GetMessage & "<i><-- Because of
shadowing, the derived class' method is not called when it is assigned
to a BaseClass variable. Instead the base class' method is
called.</i>")
Response.Write("<br>--------------<br>")

'Call child's GetMessage
Response.Write(oInherits.GetMessage)

'Assign Child instance to a base datatype
oBase = oInherits

Response.Write("<br>Call method on derived class stored in
BaseClass datatype, ")
Response.Write(oBase.GetMessage & "<i><-- Because of
overriding, the derived class' method is called even when it is
assigned to a BaseClass variable.</i>")
Response.Write("<br>")
Nov 20 '05 #1
3 1975
Good point...
I think Shadows was somehow meant to allow you to override what the designer
didn't want you to override but not change the class for real--which is the
effect you are experiencing....

Shane
"flat_ross" <fl*******@yahoo.com> wrote in message
news:19**************************@posting.google.c om...
For anyone who is just getting into VB.NET and/or is starting to work
with inheritance I would like to point out a potential pitfall. We
found this confusion recently when code-reviewing an application. If
you have not used the keyword 'Overridable' then read on for sure...

If you setup a child class and you want to override a method in your
base class, you may see the squiggles under your child's method name.
The pop-up/build error says something like:
C:\Projects\TestShadows\Ball.vb(28): function 'GetMessage' conflicts
with function 'GetMessage' in the base class 'BaseClass' and so should
be declared 'Shadows'.

To fix this, you may go ahead and put the 'Shadows' keyword into your
function declaration and, voila, it works. You run your project and
the overriden (so you think) method fires perfectly.

Unfortunately, this 'Shadows' keyword does not truly provide
inheritance (in my opinion). If you decide to get into polymorphism
then you'll quickly see the problem. If you have an instance of your
child class stored in a datatype defined as your base class and then
call the 'Shadowed' method, then the instance will actually call the
base class' method and not your 'Shadowed' method. I would hazard a
guess that if you are just starting out then this is not the
functionality you would expect. I would assume that you'd expect the
'Shadowed' method. I am not sure what other code situations would
highlight this discrepancy; maybe this is the only one so if you never
plan to do it then you'll never have a problem. But I've learned never
say never...

To fix this, the proper way to setup methods for inheritance is to
setup the base class' method with the keyword 'Overridable' and then
setup the child's method as 'Overrides'.

An explanation of 'Shadowing' is in the VS2003 help:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2004APR.1033/vbcn7/html/vbconShadowing.htm
.. If you can't find this file then click on F1 while your cursor is over
the Shadow keyword. Once help is up, scroll down and click the
'Shadowing' link under See Also. Even though it gives an in-depth
discussion on the concept it reads like it was written by a lawyer.
Only until I wrote up some demo code was I able to see the
ramifications.

I guess there are situations where Shadows may be of use but I think
it just confuses the situation (although again, never say never). The
only example I can think of is if you were upgrading the base class
and wanted to protect the child class' interface. But then you'd
probably have a good idea of what implications Shadowing has and your
object-oriented design would key in on that. Although I'm no OO
expert, I have no idea where Shadowing fits into the design anyway. I
would think designing Interfaces would come into play way long before
Shadowing.

Bottom line is I just wanted to point out that you may be incorrectly
using Shadows where you should be using Overridable/Overrides. I think
the compiler should have been designed to suggest using
Overridable/Overrides before ever suggesting Shadows.

Following is a code sample I used to demo the issue:

1) Create a web or win project, my example is a web project.
2) Add a class file to the project.
3) Make sure the new class file is empty and paste this content:
Public Class BaseClass
Public Overridable Function GetMessage() As String
Return "In BaseClass."
End Function
End Class

Public Class ShadowsMethod
Inherits BaseClass

Public Shadows Function GetMessage() As String
Return "In ShadowingMethodClass."
End Function
End Class

Public Class OverridesMethod
Inherits BaseClass

Public Overrides Function GetMessage() As String
Return "In OverridesMethod."
End Function
End Class

4) This code is for the page_load of a web form. If you want to use a
winform, you'll have to modify the Response.Writes to methods that
write to something on a winform (textbox, listbox, label, etc.)
Dim oInherits As New OverridesMethod
Dim oShadows As New ShadowsMethod
Dim oBase As BaseClass

'Call child's GetMessage
Response.Write(oShadows.GetMessage)

'Assign Child instance to a base datatype
oBase = oShadows

Response.Write("<br>Call method on derived class stored in
BaseClass datatype: ")
Response.Write(oBase.GetMessage & "<i><-- Because of
shadowing, the derived class' method is not called when it is assigned
to a BaseClass variable. Instead the base class' method is
called.</i>")
Response.Write("<br>--------------<br>")

'Call child's GetMessage
Response.Write(oInherits.GetMessage)

'Assign Child instance to a base datatype
oBase = oInherits

Response.Write("<br>Call method on derived class stored in
BaseClass datatype, ")
Response.Write(oBase.GetMessage & "<i><-- Because of
overriding, the derived class' method is called even when it is
assigned to a BaseClass variable.</i>")
Response.Write("<br>")

Nov 20 '05 #2
I always found Shadows keyword confusing too.

Most books touch on the topic you are bringing up and tell you to beware
when using Shadows.

Programming VB.NET: A Guide For Experienced Programmers puts it this way:
"If you use the Shadows keyword, members of objects get called by the kind
of container the object is stored in, not by what their ultimate types are."

I too would like to hear more about when Shadowing should and should not be
used.

Greg

"flat_ross" <fl*******@yahoo.com> wrote in message
news:19**************************@posting.google.c om...
For anyone who is just getting into VB.NET and/or is starting to work
with inheritance I would like to point out a potential pitfall. We
found this confusion recently when code-reviewing an application. If
you have not used the keyword 'Overridable' then read on for sure...

If you setup a child class and you want to override a method in your
base class, you may see the squiggles under your child's method name.
The pop-up/build error says something like:
C:\Projects\TestShadows\Ball.vb(28): function 'GetMessage' conflicts
with function 'GetMessage' in the base class 'BaseClass' and so should
be declared 'Shadows'.

To fix this, you may go ahead and put the 'Shadows' keyword into your
function declaration and, voila, it works. You run your project and
the overriden (so you think) method fires perfectly.

Unfortunately, this 'Shadows' keyword does not truly provide
inheritance (in my opinion). If you decide to get into polymorphism
then you'll quickly see the problem. If you have an instance of your
child class stored in a datatype defined as your base class and then
call the 'Shadowed' method, then the instance will actually call the
base class' method and not your 'Shadowed' method. I would hazard a
guess that if you are just starting out then this is not the
functionality you would expect. I would assume that you'd expect the
'Shadowed' method. I am not sure what other code situations would
highlight this discrepancy; maybe this is the only one so if you never
plan to do it then you'll never have a problem. But I've learned never
say never...

To fix this, the proper way to setup methods for inheritance is to
setup the base class' method with the keyword 'Overridable' and then
setup the child's method as 'Overrides'.

An explanation of 'Shadowing' is in the VS2003 help:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2004APR.1033/vbcn7/html/vbconShadowing.htm
.. If you can't find this file then click on F1 while your cursor is over
the Shadow keyword. Once help is up, scroll down and click the
'Shadowing' link under See Also. Even though it gives an in-depth
discussion on the concept it reads like it was written by a lawyer.
Only until I wrote up some demo code was I able to see the
ramifications.

I guess there are situations where Shadows may be of use but I think
it just confuses the situation (although again, never say never). The
only example I can think of is if you were upgrading the base class
and wanted to protect the child class' interface. But then you'd
probably have a good idea of what implications Shadowing has and your
object-oriented design would key in on that. Although I'm no OO
expert, I have no idea where Shadowing fits into the design anyway. I
would think designing Interfaces would come into play way long before
Shadowing.

Bottom line is I just wanted to point out that you may be incorrectly
using Shadows where you should be using Overridable/Overrides. I think
the compiler should have been designed to suggest using
Overridable/Overrides before ever suggesting Shadows.

Following is a code sample I used to demo the issue:

1) Create a web or win project, my example is a web project.
2) Add a class file to the project.
3) Make sure the new class file is empty and paste this content:
Public Class BaseClass
Public Overridable Function GetMessage() As String
Return "In BaseClass."
End Function
End Class

Public Class ShadowsMethod
Inherits BaseClass

Public Shadows Function GetMessage() As String
Return "In ShadowingMethodClass."
End Function
End Class

Public Class OverridesMethod
Inherits BaseClass

Public Overrides Function GetMessage() As String
Return "In OverridesMethod."
End Function
End Class

4) This code is for the page_load of a web form. If you want to use a
winform, you'll have to modify the Response.Writes to methods that
write to something on a winform (textbox, listbox, label, etc.)
Dim oInherits As New OverridesMethod
Dim oShadows As New ShadowsMethod
Dim oBase As BaseClass

'Call child's GetMessage
Response.Write(oShadows.GetMessage)

'Assign Child instance to a base datatype
oBase = oShadows

Response.Write("<br>Call method on derived class stored in
BaseClass datatype: ")
Response.Write(oBase.GetMessage & "<i><-- Because of
shadowing, the derived class' method is not called when it is assigned
to a BaseClass variable. Instead the base class' method is
called.</i>")
Response.Write("<br>--------------<br>")

'Call child's GetMessage
Response.Write(oInherits.GetMessage)

'Assign Child instance to a base datatype
oBase = oInherits

Response.Write("<br>Call method on derived class stored in
BaseClass datatype, ")
Response.Write(oBase.GetMessage & "<i><-- Because of
overriding, the derived class' method is called even when it is
assigned to a BaseClass variable.</i>")
Response.Write("<br>")

Nov 20 '05 #3
It seems to me sort of a kludgy way to force a class to do something that
the class designer didn't want to allow-but only for the next generation and
not further generations....so I guess it would have an immediate benefit in
that case...but not if you want to continue down the inheritance chain with
the class you derive and use shadows in.
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:eF**************@TK2MSFTNGP11.phx.gbl...
I always found Shadows keyword confusing too.

Most books touch on the topic you are bringing up and tell you to beware
when using Shadows.

Programming VB.NET: A Guide For Experienced Programmers puts it this way:
"If you use the Shadows keyword, members of objects get called by the kind
of container the object is stored in, not by what their ultimate types are."
I too would like to hear more about when Shadowing should and should not be used.

Greg

"flat_ross" <fl*******@yahoo.com> wrote in message
news:19**************************@posting.google.c om...
For anyone who is just getting into VB.NET and/or is starting to work
with inheritance I would like to point out a potential pitfall. We
found this confusion recently when code-reviewing an application. If
you have not used the keyword 'Overridable' then read on for sure...

If you setup a child class and you want to override a method in your
base class, you may see the squiggles under your child's method name.
The pop-up/build error says something like:
C:\Projects\TestShadows\Ball.vb(28): function 'GetMessage' conflicts
with function 'GetMessage' in the base class 'BaseClass' and so should
be declared 'Shadows'.

To fix this, you may go ahead and put the 'Shadows' keyword into your
function declaration and, voila, it works. You run your project and
the overriden (so you think) method fires perfectly.

Unfortunately, this 'Shadows' keyword does not truly provide
inheritance (in my opinion). If you decide to get into polymorphism
then you'll quickly see the problem. If you have an instance of your
child class stored in a datatype defined as your base class and then
call the 'Shadowed' method, then the instance will actually call the
base class' method and not your 'Shadowed' method. I would hazard a
guess that if you are just starting out then this is not the
functionality you would expect. I would assume that you'd expect the
'Shadowed' method. I am not sure what other code situations would
highlight this discrepancy; maybe this is the only one so if you never
plan to do it then you'll never have a problem. But I've learned never
say never...

To fix this, the proper way to setup methods for inheritance is to
setup the base class' method with the keyword 'Overridable' and then
setup the child's method as 'Overrides'.

An explanation of 'Shadowing' is in the VS2003 help:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2004APR.1033/vbcn7/html/vbconShadowing.htm .
If you can't find this file then click on F1 while your cursor is over
the Shadow keyword. Once help is up, scroll down and click the
'Shadowing' link under See Also. Even though it gives an in-depth
discussion on the concept it reads like it was written by a lawyer.
Only until I wrote up some demo code was I able to see the
ramifications.

I guess there are situations where Shadows may be of use but I think
it just confuses the situation (although again, never say never). The
only example I can think of is if you were upgrading the base class
and wanted to protect the child class' interface. But then you'd
probably have a good idea of what implications Shadowing has and your
object-oriented design would key in on that. Although I'm no OO
expert, I have no idea where Shadowing fits into the design anyway. I
would think designing Interfaces would come into play way long before
Shadowing.

Bottom line is I just wanted to point out that you may be incorrectly
using Shadows where you should be using Overridable/Overrides. I think
the compiler should have been designed to suggest using
Overridable/Overrides before ever suggesting Shadows.

Following is a code sample I used to demo the issue:

1) Create a web or win project, my example is a web project.
2) Add a class file to the project.
3) Make sure the new class file is empty and paste this content:
Public Class BaseClass
Public Overridable Function GetMessage() As String
Return "In BaseClass."
End Function
End Class

Public Class ShadowsMethod
Inherits BaseClass

Public Shadows Function GetMessage() As String
Return "In ShadowingMethodClass."
End Function
End Class

Public Class OverridesMethod
Inherits BaseClass

Public Overrides Function GetMessage() As String
Return "In OverridesMethod."
End Function
End Class

4) This code is for the page_load of a web form. If you want to use a
winform, you'll have to modify the Response.Writes to methods that
write to something on a winform (textbox, listbox, label, etc.)
Dim oInherits As New OverridesMethod
Dim oShadows As New ShadowsMethod
Dim oBase As BaseClass

'Call child's GetMessage
Response.Write(oShadows.GetMessage)

'Assign Child instance to a base datatype
oBase = oShadows

Response.Write("<br>Call method on derived class stored in
BaseClass datatype: ")
Response.Write(oBase.GetMessage & "<i><-- Because of
shadowing, the derived class' method is not called when it is assigned
to a BaseClass variable. Instead the base class' method is
called.</i>")
Response.Write("<br>--------------<br>")

'Call child's GetMessage
Response.Write(oInherits.GetMessage)

'Assign Child instance to a base datatype
oBase = oInherits

Response.Write("<br>Call method on derived class stored in
BaseClass datatype, ")
Response.Write(oBase.GetMessage & "<i><-- Because of
overriding, the derived class' method is called even when it is
assigned to a BaseClass variable.</i>")
Response.Write("<br>")


Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: gokul | last post by:
Hi, Iam a newbie to dotnet and I experience problems in using the Browser control in VB .net. Though Iam able to use it with its basic features, I need to customise it. ...
37
by: hasadh | last post by:
Hello, probably this may be a simple qn to u all but I need an answer plz. In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who included this code as a library into his module...
2
by: Nic | last post by:
Hi, The original creators of the ListView control never envisioned that we'd want to override the Columns property with our own version, so they didn't mark it as Overridable. This means we...
10
by: Özden Irmak | last post by:
Hi, I'm trying to hide an event of my custom usercontrol derived control, something like "Shadows" in VB.Net, but although the event is hidden from PropertyGrid, from CodeEditor I can still...
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
7
by: Satish | last post by:
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
4
by: Stephen Costanzo | last post by:
My goal is to have a property or function that returns different types of returns with no initial arguments. For example Public Class Something Private sData as string Private lData as long ...
1
by: trialproduct2004 | last post by:
Hi all, can any one tell me use of shadow keyword in vb.net and its equivalent in c# with e.g. Please help me. thanks in advance.
3
by: Mike S | last post by:
Maybe I'm missing some fundamental unwritten law of OOP, but I was wondering why the VB.NET compiler doesn't take advantage of the fact that all .NET objects, being derived from the Object base...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.