473,509 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raising events

I am developing a somewhat complex component at the moment and coincidently
I am also reading the Framework Design Guidelines book.

After reading the section about event raising I have re-written the way my
component raises events to follow the Framework Design Guides verbatim; ie
(object sender, EventArgs (or some subclass there of) e).

The thing that was not explained is why should I need to cast the sender to
'object' when I know very well that it will be of type 'MyClass' or a
subclass there of.

By setting it to 'object' it makes more difficult for the consumer of the
event since they need to explicitly cast it to MyClass to do anything useful
with it.

If I were to declare the delegate as (MyClass sender, ...) then the consumer
would only need to explicitly cast it when they were dealing with a subclass
of MyClass.

By setting it to 'object' are we taking framework design guidelines just a
bit too far? Or is there some other benefit that I do not appreciate.

PS Pages 132 - 138, particular P138 (second 'do')

Thanks
Dave A

Dec 12 '05 #1
4 1581
I *think* it's more to keep eventhandlers reusable. If this doesn't apply
in your case, I'd use the specific class type - I have in the past.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message
news:OD*************@TK2MSFTNGP09.phx.gbl...
I am developing a somewhat complex component at the moment and coincidently
I am also reading the Framework Design Guidelines book.

After reading the section about event raising I have re-written the way my
component raises events to follow the Framework Design Guides verbatim; ie
(object sender, EventArgs (or some subclass there of) e).

The thing that was not explained is why should I need to cast the sender
to 'object' when I know very well that it will be of type 'MyClass' or a
subclass there of.

By setting it to 'object' it makes more difficult for the consumer of the
event since they need to explicitly cast it to MyClass to do anything
useful with it.

If I were to declare the delegate as (MyClass sender, ...) then the
consumer would only need to explicitly cast it when they were dealing with
a subclass of MyClass.

By setting it to 'object' are we taking framework design guidelines just a
bit too far? Or is there some other benefit that I do not appreciate.

PS Pages 132 - 138, particular P138 (second 'do')

Thanks
Dave A

Dec 12 '05 #2
Hi,
Microsoft event framework is such because many events have the same delegate like Click and GotFocus and some other has EventHandler is as a delegate also both event can be fired by any Button, or other lots of controls so it needs to be there "object" which can give you facility to use it with multiple events as well as multiple type of Control objects.

But if its not in your case, means only single type of requirement is there and its for only one type of Class or Interface then you just create your own Delegate with your specific sender Type and its also not required to have EventArgs, just pass the parameters or a bundle of parameter as some object of some collection type.
It will definately work.

Sample according to publisher-subscriber scenario in Observer design pattern:

On publisher side:
Delegate:
public void delegate MyDelegate(MyType sender, MyParamCollection params);
Event in MyType:
public event MyDelegate MyEvent;
define some method for raising event like,
private void OnMyEvent() {
MyParamCollection coll = new MyParamCollection();
:
:
if(MyEvent !=null) {
MyEvent(this, coll);
}
}
and call this method at some point where you want to raise the specific event,

On subscriber side:
objMyType.MyEvent += MyDelegate(MyEventHandlerMethod);
private void MyEventHandlerMethod(MyType sender, MyParamCollection params) {
do something.....
}

HTH,

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message news:OD*************@TK2MSFTNGP09.phx.gbl...
I am developing a somewhat complex component at the moment and coincidently
I am also reading the Framework Design Guidelines book.

After reading the section about event raising I have re-written the way my
component raises events to follow the Framework Design Guides verbatim; ie
(object sender, EventArgs (or some subclass there of) e).

The thing that was not explained is why should I need to cast the sender to
'object' when I know very well that it will be of type 'MyClass' or a
subclass there of.

By setting it to 'object' it makes more difficult for the consumer of the
event since they need to explicitly cast it to MyClass to do anything useful
with it.

If I were to declare the delegate as (MyClass sender, ...) then the consumer
would only need to explicitly cast it when they were dealing with a subclass
of MyClass.

By setting it to 'object' are we taking framework design guidelines just a
bit too far? Or is there some other benefit that I do not appreciate.

PS Pages 132 - 138, particular P138 (second 'do')

Thanks
Dave A


Dec 12 '05 #3
Thank you. However I would like to argue your following point...
and its also not required to have EventArgs, just pass the parameters or a bundle of parameter as some object of some collection type. It will definately work.
I don't doubt that this will work - but as it is explained in the book if you do adopt the "object sender, subclass of EventArgs e" approach then you are effectively future proofing your design since any future addition to your "subclass of EventArgs" class will not break any compatibility with previous releases. Following your suggestion of "just pass the parameters or a bundle of parameter as some object of some collection type" may not provide as much flexibility for future designs.

The design pattern that you describe below contradicts the Framework Design Guidelines book.

Regards
Dave A

"Mahesh Devjibhai Dhola [MVP]" <dh*********@hotmail.com> wrote in message news:eR**************@TK2MSFTNGP11.phx.gbl...
Hi,
Microsoft event framework is such because many events have the same delegate like Click and GotFocus and some other has EventHandler is as a delegate also both event can be fired by any Button, or other lots of controls so it needs to be there "object" which can give you facility to use it with multiple events as well as multiple type of Control objects.

But if its not in your case, means only single type of requirement is there and its for only one type of Class or Interface then you just create your own Delegate with your specific sender Type and its also not required to have EventArgs, just pass the parameters or a bundle of parameter as some object of some collection type.
It will definately work.

Sample according to publisher-subscriber scenario in Observer design pattern:

On publisher side:
Delegate:
public void delegate MyDelegate(MyType sender, MyParamCollection params);
Event in MyType:
public event MyDelegate MyEvent;
define some method for raising event like,
private void OnMyEvent() {
MyParamCollection coll = new MyParamCollection();
:
:
if(MyEvent !=null) {
MyEvent(this, coll);
}
}
and call this method at some point where you want to raise the specific event,

On subscriber side:
objMyType.MyEvent += MyDelegate(MyEventHandlerMethod);
private void MyEventHandlerMethod(MyType sender, MyParamCollection params) {
do something.....
}

HTH,

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message news:OD*************@TK2MSFTNGP09.phx.gbl... I am developing a somewhat complex component at the moment and coincidently
I am also reading the Framework Design Guidelines book.

After reading the section about event raising I have re-written the way my
component raises events to follow the Framework Design Guides verbatim; ie
(object sender, EventArgs (or some subclass there of) e).

The thing that was not explained is why should I need to cast the sender to
'object' when I know very well that it will be of type 'MyClass' or a
subclass there of.

By setting it to 'object' it makes more difficult for the consumer of the
event since they need to explicitly cast it to MyClass to do anything useful
with it.

If I were to declare the delegate as (MyClass sender, ...) then the consumer
would only need to explicitly cast it when they were dealing with a subclass
of MyClass.

By setting it to 'object' are we taking framework design guidelines just a
bit too far? Or is there some other benefit that I do not appreciate.

PS Pages 132 - 138, particular P138 (second 'do')

Thanks
Dave A


Dec 12 '05 #4
Hi,
I dont know about the book you have mentioned but as i mentioned that sender object and subclass of EventArg is facilities provided in Microsoft Event framework BUT IN CASE YOU WANT and if its requirement of your problem solution then you can use your custom type instead of object sender and also same as with EventArgs.
I normally, uses the same framework with custom EventArgs which have my required parameters or collection of it.
And for future perspective, you may right but i dont see any reason that Microsoft disallow this custome usage feature in FUTURE product... then also yes, its definately preferrable to follow such framework.
Finally, its all depends of problem type and solution requirements.

HTH,

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message news:u7**************@TK2MSFTNGP15.phx.gbl...
Thank you. However I would like to argue your following point...
and its also not required to have EventArgs, just pass the parameters or a bundle of parameter as some object of some collection type. It will definately work.
I don't doubt that this will work - but as it is explained in the book if you do adopt the "object sender, subclass of EventArgs e" approach then you are effectively future proofing your design since any future addition to your "subclass of EventArgs" class will not break any compatibility with previous releases. Following your suggestion of "just pass the parameters or a bundle of parameter as some object of some collection type" may not provide as much flexibility for future designs.

The design pattern that you describe below contradicts the Framework Design Guidelines book.

Regards
Dave A

"Mahesh Devjibhai Dhola [MVP]" <dh*********@hotmail.com> wrote in message news:eR**************@TK2MSFTNGP11.phx.gbl...
Hi,
Microsoft event framework is such because many events have the same delegate like Click and GotFocus and some other has EventHandler is as a delegate also both event can be fired by any Button, or other lots of controls so it needs to be there "object" which can give you facility to use it with multiple events as well as multiple type of Control objects.

But if its not in your case, means only single type of requirement is there and its for only one type of Class or Interface then you just create your own Delegate with your specific sender Type and its also not required to have EventArgs, just pass the parameters or a bundle of parameter as some object of some collection type.
It will definately work.

Sample according to publisher-subscriber scenario in Observer design pattern:

On publisher side:
Delegate:
public void delegate MyDelegate(MyType sender, MyParamCollection params);
Event in MyType:
public event MyDelegate MyEvent;
define some method for raising event like,
private void OnMyEvent() {
MyParamCollection coll = new MyParamCollection();
:
:
if(MyEvent !=null) {
MyEvent(this, coll);
}
}
and call this method at some point where you want to raise the specific event,

On subscriber side:
objMyType.MyEvent += MyDelegate(MyEventHandlerMethod);
private void MyEventHandlerMethod(MyType sender, MyParamCollection params) {
do something.....
}

HTH,

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message news:OD*************@TK2MSFTNGP09.phx.gbl... I am developing a somewhat complex component at the moment and coincidently
I am also reading the Framework Design Guidelines book.

After reading the section about event raising I have re-written the way my
component raises events to follow the Framework Design Guides verbatim; ie
(object sender, EventArgs (or some subclass there of) e).

The thing that was not explained is why should I need to cast the sender to
'object' when I know very well that it will be of type 'MyClass' or a
subclass there of.

By setting it to 'object' it makes more difficult for the consumer of the
event since they need to explicitly cast it to MyClass to do anything useful
with it.

If I were to declare the delegate as (MyClass sender, ...) then the consumer
would only need to explicitly cast it when they were dealing with a subclass
of MyClass.

By setting it to 'object' are we taking framework design guidelines just a
bit too far? Or is there some other benefit that I do not appreciate.

PS Pages 132 - 138, particular P138 (second 'do')

Thanks
Dave A


Dec 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2070
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
6
2836
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
17252
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
7
2429
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
0
1714
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
0
1430
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
2
1679
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
0
1240
by: Apu Nahasapeemapetilon | last post by:
Suggestions on cross-posting this issue would be appreciated. I've got a C# .NET DLL (CLR v1.1) that raises events into its container. When the container is a .NET application, raising the...
7
2860
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.