Delegates and inheritance
-
Hi, I have the following strange behaviour I have an abstract base class in which a delegate is availlable. I the derived class i need to call the connected function. But that results in the following compliation error: The event 'winFromsTest.MyBase.Callback' can only appear on the left hand side of += or -= (except when used from within the type 'winFromsTest.MyBase') Does anybody knows a reasson why i can't call the delegate from the derived class('s) codito ergo sum
-
Hi, I have the following strange behaviour I have an abstract base class in which a delegate is availlable. I the derived class i need to call the connected function. But that results in the following compliation error: The event 'winFromsTest.MyBase.Callback' can only appear on the left hand side of += or -= (except when used from within the type 'winFromsTest.MyBase') Does anybody knows a reasson why i can't call the delegate from the derived class('s) codito ergo sum
Because you have declared it as an event which is protected from invalid access. If you want to raise the event from an inheriting class then add a protected method to your base class as a helper function:
//base class
protected virtual void OnCallback(EventArgs e){
if (Callback != null)
Callback(this, e);
}//somewhere in inheriting class
base.OnCallback(EventArgs.Empty); -
Because you have declared it as an event which is protected from invalid access. If you want to raise the event from an inheriting class then add a protected method to your base class as a helper function:
//base class
protected virtual void OnCallback(EventArgs e){
if (Callback != null)
Callback(this, e);
}//somewhere in inheriting class
base.OnCallback(EventArgs.Empty);