Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shadowing events in .NET

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?

like image 624
dnatoli Avatar asked Sep 08 '09 02:09

dnatoli


People also ask

What is shadowing in C #?

Shadowing is also known as method hiding. The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function. Use the new keyword to perform shadowing and to create the own version of the base class function.

What is difference between shadowing and overriding in C#?

Shadowing redefines the complete method, whereas overriding redefines only the implementation of the method. In Overriding, you can access the base class using the child class' object overridden method.. Shadowing has cannot access the chaild class methos. Shadowing is also known as method hiding.

What is shadowing in Visual Basic?

When two programming elements share the same name, one of them can hide, or shadow, the other one. In such a situation, the shadowed element is not available for reference; instead, when your code uses the element name, the Visual Basic compiler resolves it to the shadowing element.

What do you mean by shadowing?

to follow someone else while they are at work in order to learn about that person's job: Your first week in the job will be spent shadowing one of our more experienced employees. SMART Vocabulary: related words and phrases. Pursuing.


1 Answers

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.

like image 113
Adam Robinson Avatar answered Oct 25 '22 08:10

Adam Robinson