views:

207

answers:

1

Hi,

In VB.NET there is a keyword 'shadows'. Let's say I have a base class called 'Jedi' and a derived class called 'Yoda' which inherits from 'Jedi'. If I declare a method in 'Jedi' called 'ForcePush' and shadow that out in 'Yoda' then when calling the method on an instance of the 'Yoda' class, it will ignore the base class implementation and use the derived class' implementation. However if I have an instance of 'Yoda' that was declared originally as of type 'Jedi', i.e. Dim j as Jedi = new Yoda(), and called the 'ForcePush' method on the instance, it will use the Jedi implementation.

Now let's say I have an event that is called 'UsingForce' which is raised when the 'ForcePush' method is called, and I shadow the event out in the derived class (this is because 'Yoda' has an interface 'IForcePowers' that declares this event) and each class raises it's respective event.

If I have an instance of 'Yoda' that is declared as type 'Jedi' (like above) and I put an event handler on the 'UsingForce' event of 'Jedi', and then the 'ForcePush' method is called in the 'Yoda' class, will this event handler be reached?

Thanks, Dane.

+1  A: 

Using the shadows keyword in VB.NET means that you are declaring an entirely new member that exists outside of any inheritance heirarchy that exists. This is why that keyword (and the associated practice) is generally regarded as "smelly" (though some people object to this particular term, I find it quite appropriate). What is the reasoning behind this pattern of "shadowing things out"? This approach is usually reserved for circumstances where there is no other way to go about accomplishing what you need. Was inheriting and overriding the methods not an option?

In any event, if you "shadow out" the event in a lower class, then no, there is no possible way for a class farther up the inheritance chain to trigger the event directly, as they have no idea that it even exists.

Adam Robinson
You can't override events, and the reason I need to implement it in the derived class is because the derived class uses an interface that exposes the event, so that when I declare and object of type 'IForcePowers' with events, I can add a static handler to the object.
link664
If an interface declares any member--be it a function, property, or event--and a member with a matching name and signature is already implemented either in the implementing class or in one of its base classes, then that member will automatically count toward the interface implementation. IE, if I were to declare an interface called IText with a Text property typed as a string, then I could inherit from any control and declare that it implements the IText interface without any additional code, since a string Text property already exists.
Adam Robinson