Shadows keyword

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
Still have no online help as it is coming to 3 weeks withou my own laptop:(

So I have come across this and hope someone can enlighten me.

Visual Basic:
Public Class clsCar

  Inherits clsVehicle
 
  sub new()

End Class

Visual Basic:
Sub New()

   MyBase.New(some guff goes here)

End Sub

My book says that the above ensures that the New method of class clsVehicle is called by using MyBase.

when a child class defines a method that already exists in the base class, any object created from the child class will call the code in the method of this class, unless we explicitly call the base class method as above.

Yep makes sense until I see and read something like this;

Visual Basic:
Public Class clsCar

  Inherits clsVehicle

  sub New()

  Shadows Sub Drive()

End Class

The keyword shadows helps the developer to extend base classes in a more flexible way, allowing the child class to block any methods with the same name defined in the base class, even if they have different parameters. In the preceeding example, if we had defined method Drive without the shadows keyword we would have two different Drive methods in the clsCar class.

I've lost the plot now?

what of MyBase or Overloads or or ??
 
Shadows is basically the same as Overrides except that it can also be applied to variables, constants, and so forth, not just methods. Also, to use Overrides, the base class method must be defined as Overrideable, whereas any method can be shadowed.
 
Overrides and shadows are a bit more complex than Squirm suggested. Overrides and Overridable allow you to write code which works with a base class but will call the sub classes overridden method.
Paste the following snippet into a new form project
Visual Basic:
Public Class BaseClass
	Public Sub ShadowNoise()
		MessageBox.Show("Class1")
	End Sub

	Public Overridable Sub OverrideNoise()
		MessageBox.Show("Class1")

	End Sub
End Class

Public Class SubClass
	Inherits BaseClass

	Public Overrides Sub OverrideNoise()
		MessageBox.Show("Class2")

	End Sub
	Public Shadows Sub ShadowNoise()
		MessageBox.Show("Class2")

	End Sub
End Class

and in the form paste this in a button handler.
Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim c As BaseClass = New SubClass()
	c.ShadowNoise()     'Notice the method called
	c.OverrideNoise()     'notice different method called
End Sub

if you put a breakpoint on this routine and step through the code notice which version of the functions get called when an override or a shadowed function is used.
 
Mmm now I'm scatching my head?

why?

Visual Basic:
         Dim c As BaseClass = New SubClass

and not?

Visual Basic:
        Dim c As SubClass = New SubClass

Surely the latter includes BaseClass as it inherits it? Or have not understood OOP at all:(
 
Yes the latter includes all the base class functionality as well.
However by declaring a variable as the BaseClass means it can contain the BaseClass or any class that inherits from the base - why write code that is limitted to only working with the sub class if I can write code that works with the base class or the sub class?
In the example I gave the decision to be a subclass or a baseclass could be made on run-time criteria, the code would work without having to know exactly which class it was working with.
 
Sorry mate I must be completely off beam here as I just don't get it??

If the latter includes the functionality of the base class then are you not limited to only working with the sub class as the base class is included in it?

I obviously haven't picked up on this as much as I thought which concerns me now:(

Could you be so kind as to post a code snippet that would detail this issue a little better. I appreciate you have already posted some code but alas I don't get it?

In hope......
 
Although a SubClass IS A Type of BaseClass the reverse is not true. You cannot declare a variable of type subclass and make it equal to a base class

if you change the button event code to the following you will see where it fails to compile
Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim baseC As BaseClass 
    baseC = new BaseClass()   'will work

    baseC.ShadowNoise()     
    baseC.OverrideNoise()     

    baseC = New SubClass()    'will work
    baseC.ShadowNoise()     
    baseC.OverrideNoise()    

    Dim subC as SubClass
    subC = New SubClass()    'will work
    subC.ShadowNoise()     
    subC.OverrideNoise()     

    subC = New BaseClass()    'Error
End Sub
[CODE=visualbasic]

Now if I was being a bit more structured rather than putting the declarations into the event handler direct I could have used a sub routine.

Based on the original sample replace the button click with the following and notice the results as you step through with a debugger.
[CODE=visualbasic]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Dim bc As New BaseClass()
	DosomeThing(bc)
	Dim sc As New SubClass()
	DosomeThing(sc)
End Sub

Private Sub DosomeThing(ByVal c As BaseClass)
	c.ShadowNoise()
	c.OverrideNoise()
End Sub

If that works try changing the function definition to
Visual Basic:
Private Sub DosomeThing(ByVal c As SubClass)

and see what happens then.
 
I do hope you can stick with me on this as it seems like a mission to try to get this but....der...nope:(

This is what I have done to help me get it, and the results I get along with the results I thought I should get

Visual Basic:
Public Class BaseClass

    Public Sub ShadowNoise()

        MessageBox.Show("BaseClass ShadowNoise")

    End Sub

    Public Overridable Sub OverrideNoise()

        MessageBox.Show("BaseClass OverrideNoise")

    End Sub

End Class

Public Class SubClass
    Inherits BaseClass

    Public Shadows Sub ShadowNoise()

        MessageBox.Show("SubClass ShadowNoise")

    End Sub

    Public Overrides Sub OverrideNoise()

        MessageBox.Show("SubClass OverrideNoise")

    End Sub

End Class

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim bc As New BaseClass

        DosomeThing(bc)

        Dim sc As New SubClass

        DosomeThing(sc)

    End Sub

    Private Sub DosomeThing(ByVal c As BaseClass)

        c.ShadowNoise()

        c.OverrideNoise()

    End Sub

I get

BaseClass ShadowNoise
BaseClass OverrideNoise
BaseClass ShadowNoise
SubClass OverrideNoise

But expected

BaseClass ShadowNoise
BaseClass OverrideNoise
SubClass ShadowNoise
SubClass OverrideNoise

Surely as SubClass shadows BaseClass's ShadowNoise shouldn't it have calles SubClass-ShadowNoise?

Sorry for being dim but surely it will give you a warm sense of satisfaction once you get the penny to drop for me :) :)
 
Shadows will hide the BaseClass version of the function from the sub class and sub-classes of it, and calling it from a variable declared as the BaseClass will result in the BaseClass version being called.
Overrides will result in the SubClasses version being called.
 
PlausiblyDamp - I think you explain it much better than Microsoft were able to, or maybe I'm just dense. You highlighted the key difference between shadowing and overriding which had previously eluded me.

Kudos!
 
Ok so now it's another day I shall try to get understand this topic. Alas this reminds me of 1985 when for the life of me I just could not understand arrays! When the penny dropped I thought...duh.....why couldn't I understand them?

:)
 
OK this is the way I see it which does not explain the output I get:(


Shadows will hide the BaseClass version of the function from the sub class and sub-classes of it, and calling it from a variable declared as the BaseClass will result in the BaseClass version being called.
Overrides will result in the SubClasses version being called.

Yes I understand this but the output does not match?

I get

BaseClass ShadowNoise
BaseClass OverrideNoise
BaseClass ShadowNoise
SubClass OverrideNoise

But expected

BaseClass ShadowNoise
BaseClass OverrideNoise
SubClass ShadowNoise
SubClass OverrideNoise

surely the output should be what I expect and not what I actually get?

For example;

Visual Basic:
Dim sc As New SubClass

DosomeThing(sc)

In this section is it right that this is happening;

SubClass.ShadowNoise is being called not BaseClass.ShadowNoise? This should be the case because the SubClass function ShadowNoise has shadowed the BaseClass version of the function and so blocks it?

So why does the output say that the BaseClass version is being called?
 
Dosomething has it's parameter typed to BaseClass - so it calls the BaseClass version. A function marked as Shadows in a SubClass does not get the SubClass' version called - that's what overridable / overrides is there for.
 
DOOOOOOOOOOOHHHHHHHHHHH!

The whole forum gets deafened as hog finally hears the penny drop!!!!!

There always has to be one.......and it's usually me:)

Thnx
 
A wonderful post... thank you for being so lost hog... if i only had seen this in time to contribute
 
Your welcome:) I like to pride myself on persisting to try to understand to the point where my brain shuts down all non essential systems at which point I pass out:):)
 
Back
Top