473,890 Members | 1,505 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 1602
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**@sigmasolu tionsdonotspamm e.com.au> wrote in message
news:OD******** *****@TK2MSFTNG P09.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(MyTy pe sender, MyParamCollecti on params);
Event in MyType:
public event MyDelegate MyEvent;
define some method for raising event like,
private void OnMyEvent() {
MyParamCollecti on coll = new MyParamCollecti on();
:
:
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.MyEve nt += MyDelegate(MyEv entHandlerMetho d);
private void MyEventHandlerM ethod(MyType sender, MyParamCollecti on params) {
do something.....
}

HTH,

"Dave A" <da**@sigmasolu tionsdonotspamm e.com.au> wrote in message news:OD******** *****@TK2MSFTNG P09.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*********@ho tmail.com> wrote in message news:eR******** ******@TK2MSFTN GP11.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(MyTy pe sender, MyParamCollecti on params);
Event in MyType:
public event MyDelegate MyEvent;
define some method for raising event like,
private void OnMyEvent() {
MyParamCollecti on coll = new MyParamCollecti on();
:
:
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.MyEve nt += MyDelegate(MyEv entHandlerMetho d);
private void MyEventHandlerM ethod(MyType sender, MyParamCollecti on params) {
do something.....
}

HTH,

"Dave A" <da**@sigmasolu tionsdonotspamm e.com.au> wrote in message news:OD******** *****@TK2MSFTNG P09.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**@sigmasolu tionsdonotspamm e.com.au> wrote in message news:u7******** ******@TK2MSFTN GP15.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*********@ho tmail.com> wrote in message news:eR******** ******@TK2MSFTN GP11.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(MyTy pe sender, MyParamCollecti on params);
Event in MyType:
public event MyDelegate MyEvent;
define some method for raising event like,
private void OnMyEvent() {
MyParamCollecti on coll = new MyParamCollecti on();
:
:
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.MyEve nt += MyDelegate(MyEv entHandlerMetho d);
private void MyEventHandlerM ethod(MyType sender, MyParamCollecti on params) {
do something.....
}

HTH,

"Dave A" <da**@sigmasolu tionsdonotspamm e.com.au> wrote in message news:OD******** *****@TK2MSFTNG P09.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
2112
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 form and I can process what I want based on them. If I try to catch raised events by this class but within a form located in a different assembly, I am not able to
6
2889
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 TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
4
17279
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 maintain the reference to any module.
7
2449
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 Manager. Your main application then hooks into the Plugin Manager. What I'm trying to figure out is how to impliment some form of raising
0
1729
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 upto a hundred user controls on a single page and AutoPostBack isn't an option. I have no problem with raising events when an user control is dropped on the aspx page.
0
1459
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 (0x80041003): Exception from HRESULT: 0x80041003. at Microsoft.EnterpriseInstrumentation.EventSinks.WmiEventSink ..Write(Object eventToRaise) at
2
1703
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 the parent form. This is fine. The problem occurs when I want to raise an event for a user clicking on one of the clsRecords which are on the grid. So I've placed: Public Event GridRecordClicked(ByVal rec As clsGridRecord,
0
1267
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 events work everytime. When the container is Internet Explorer, raising the events work on some PCs, but generate the exception: "object does not match target type" on some other PCs.
7
2880
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 in the label. I tried by raising events as used in Winforms but it is not as easy as
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11232
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8016
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5851
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.