|
-
Oct 26th, 2006, 08:52 AM
#1
Thread Starter
Hyperactive Member
throwing events of a baseclass is impossible?!
I just noticed inherited classes can not throw public events of the baseclass...
Is this correct, or am I missing something? But what interests me more: WHY is it not allowed?
I don't see a reason for this, since it can just be avoided like this.:
Code:
event EventHandler myevent;
protected void ThrowMyEvent(){
if (myevent != null)
myevent();
}
Thank you in advance
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Oct 26th, 2006, 09:19 AM
#2
Re: throwing events of a baseclass is impossible?!
Firstly, you don't throw events. You raise events and you throw exceptions.
Secondly, there should never be a reason for a derived class to raise a base class's event directly, e.g. Form.Load event and Form.OnLoad method. If you declare an event in a class then you should also declare a corresponding protected method that raises that event. If the derived class wants to raise the event it just calls the method of the base class and the method raises the event. That's the way it is supposed to be done, so whether or not a derived class can raise the base class's events it never should.
-
Oct 26th, 2006, 10:18 AM
#3
Thread Starter
Hyperactive Member
Re: throwing events of a baseclass is impossible?!
"What's in a name". Maybe you can not throw events, but I can .
Yeah, now that you mention it, indeed those graphical components always have both Onblablabla functions and Blablabla events I had kind of forgotten those silly Onblablabla functions.
So I should just call my crappy little function : "OnBlablabla"
Code:
event EventHandler myevent;
protected void OnMyEvent(){
if (myevent != null)
myevent();
}
Thanks for sharing your insights jmcilhinney.
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Oct 26th, 2006, 09:18 PM
#4
Re: throwing events of a baseclass is impossible?!
I was thinking, I haven't tested this but if indeed it is the case that a derived class cannot directly raise an event of its base class then I would think the reson is as follows. As I said, and you said yourself in your first post, you can raise an event of the base class by calling the corresponding OnSomething method. What this does is ensure that any actions that are taken in that method occur every time the event is raised. The code would be placed in that method specifically because it was supposed to be executed when the corresponding event was raised, so by forcing a derived class to call that method you inherently ensure that that code is always executed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|