Child/Parent event raising
You can attach to the event from the child instance.
public abstract class Parent
{
public event Action Something;
public void OnSomething()
{
if (Something != null)
{
Something();
}
}
}
public class Child : Parent
{
}
Child c = new Child();
c.Something += () => Console.WriteLine("Got event from child");
c.OnSomething();
> Got event from child
You can even declare it as a Parent
type that contains a child:
Parent c2 = new Child();
c2.Something += () => Console.WriteLine("Got event from Parent type");
c2.OnSomething();
> Got event from Parent type
An abstract class is just a code template that gets copied into every class that inherits from it (to put it simply). Think of it like, all of your Child
classes contain an identical copy of the code that exists in Parent
.
Note that this will also produce a unique event handler for each instance of Child
. Having a static event handler for all Child
s that derive from Parent
would look like this, and requires no code in Child
:
public abstract class Parent
{
public static event Action Something;
public static void OnSomething()
{
if (Something != null)
{
Something();
}
}
}
Then, you could do something like this, for example:
Parent.Something += () => Console.WriteLine("This will be invoked twice.");
Child c = new Child();
Child c2 = new Child();
c.OnSomething();
c2.OnSomething();
> This will be invoked twice.
> This will be invoked twice.
Both of those objects/event calls will invoke the same event handler even though they come from separate children.
mo alaz
Updated on June 04, 2022Comments
-
mo alaz almost 2 years
I have a parent abstract class with several children classes. Eventually, I would like the progress done in the children classes to be shown via a progress bar in the GUI.
What I currently have done right now, which I am realizing will not work, is the event method definition declared in the parent class as a virtual method which each child class will overwrite. So something like :
public abstract class Parent { public event EventHandler someEvent; protected virtual void OnSomeEvent(object sender, EventArgs e) { EventHandler eh= someEvent; if (eh!= null) { eh(this, e); } } }
And my child classes have something like :
protected override void OnSomeEvent(object sender, EventArgs e) { base.OnSomeEvent(sender, e); }
and the event is raised somewhere in the child class.
However, seeing as the parent class is abstract, I will not be able to listen to the event from my GUI because I can not create an instance of an abstract class.
Am I completely off course and/or is there another method of doing this?
-
qJake almost 11 yearsWhy can't your UI attach to the event on the child instance? If you inherit from
Parent
,Child
will have the event, too. -
mo alaz almost 11 yearsThe idea is that I don't necessarily want the GUI to know which child raised the event. @SpikeX
-
qJake almost 11 yearsSo you want a static event handler on Parent, that only has one instance, is what you're saying.
-
mo alaz almost 11 yearsI want a progress bar for each child so your solution was just right @SpikeX
-
Osama AbuSitta over 7 yearsPossible duplicate of c# : accessing parent class's events not possible?
-
-
mo alaz almost 11 yearsJust to be clear, what is your implementation of OnSomething() in the child class?
-
qJake almost 11 years@moalaz There isn't one. It inherits from
Parent
automatically. You can invokeOnSomething();
from withinChild
, unless you specifically want to override it with another behavior, but by default, it will just invoke theSomething
event. -
qJake almost 11 years@moalaz I also added some code for a static event if you didn't want the event handler to be unique to each
Child
instance.