473,480 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wiring Up An Event

I have several instances where I wire up an event handler like this:

// Instantiate object
childClass parentObject = new childClass();

// Wire up event
parentObject.SomeChildEvent += new EventHandler(ParentEventHandler);

What I'm wondering though is if there's a way with dotNet 1.1 for me to
somehow pass the necessary information contained in the 2nd line of code to a
separate class and have the wiring occur there? This would mean that if the
event is fired it'll still be handled by 'ParentEventHandler' in the parent
class.

I tried experimenting with delegates to do this but just couldn't figure it
out. Is it possible to do?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
3 4081
Robert W. <Ro*****@discussions.microsoft.com> wrote:
I have several instances where I wire up an event handler like this:

// Instantiate object
childClass parentObject = new childClass();

// Wire up event
parentObject.SomeChildEvent += new EventHandler(ParentEventHandler);

What I'm wondering though is if there's a way with dotNet 1.1 for me to
somehow pass the necessary information contained in the 2nd line of code to a
separate class and have the wiring occur there?


Well, you can pass the delegate through as a parameter (of type
EventHandler) very easily.

You can't easily pass the event itself though - the other class would
need to know that it's the SomeChildEvent that needs wiring up.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have several instances where I wire up an event handler like this:

// Instantiate object
childClass parentObject = new childClass();

// Wire up event
parentObject.SomeChildEvent += new EventHandler(ParentEventHandler);

What I'm wondering though is if there's a way with dotNet 1.1 for me to
somehow pass the necessary information contained in the 2nd line of code to a separate class and have the wiring occur there? This would mean that if the event is fired it'll still be handled by 'ParentEventHandler' in the parent class.


// Instantiate object
childClass parentObject = new childClass();

// Create event handler.
EventHandler myHandler = new EventHandler(ParentEventHandler);

// Call some method to wire up event handler to object.
WireUpSomeChildEvent(parentObject,myHandler);

// Method to wire up an event handler on an object.
public void WireUpSomeChildEvent(childClass aObject, EventHandler aHandler)
{
aObject.SomeChildEvent += aHandler;
}
Nov 17 '05 #3
Jon, Scott,

Thank you! And Scott, your sample code was excellent!

You know, just when I think I'm becoming more proficient I seem to run into
these little things that just baffle me. And then I see such simple, elegant
solutions and it all becomes clear again. Oh well, one day I'll have this
all figured out!! :-)

--
Robert W.
Vancouver, BC
www.mwtech.com

"Scott Roberts" wrote:

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have several instances where I wire up an event handler like this:

// Instantiate object
childClass parentObject = new childClass();

// Wire up event
parentObject.SomeChildEvent += new EventHandler(ParentEventHandler);

What I'm wondering though is if there's a way with dotNet 1.1 for me to
somehow pass the necessary information contained in the 2nd line of code

to a
separate class and have the wiring occur there? This would mean that if

the
event is fired it'll still be handled by 'ParentEventHandler' in the

parent
class.


// Instantiate object
childClass parentObject = new childClass();

// Create event handler.
EventHandler myHandler = new EventHandler(ParentEventHandler);

// Call some method to wire up event handler to object.
WireUpSomeChildEvent(parentObject,myHandler);

// Method to wire up an event handler on an object.
public void WireUpSomeChildEvent(childClass aObject, EventHandler aHandler)
{
aObject.SomeChildEvent += aHandler;
}

Nov 17 '05 #4

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

Similar topics

0
1403
by: Chris Fink | last post by:
I am writing a c# unattended windows application and I would like to write an event to handle any type of application error by gracefully degrading and writing a message to an error log. Errors...
0
1943
by: Plumer | last post by:
A warm, south pacific good morning to everyone I posted this message on one of the other MSDN newsgroups (Messaging & Collaboration) and realised later that perhaps it wasn't the best choice. If I...
0
1245
by: Robert W. | last post by:
Though I'm new to C#, I've successfully built an MVC (Model-View-Controller) Framework - with much thanks to the help of the great people on here! Anyhow, my MVC Framework, which I call "ASM" for...
7
3250
by: Oisin Grehan | last post by:
Hi, I have a UserControl derived class: <ns:votingbutton runat="server" id="btn1" onclick="votingbuttonclick" /> My question is, what code do I need in place in the codebehind for this to...
0
1113
by: Homam | last post by:
To wire a Click event handler to a dynamically-added control (e.g. LinkButton), you need to do so during the Load event of the parent (e.g. a PlaceHolder). The problem is, the Click event...
4
3000
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime,...
5
3062
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
12
13405
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
1
1542
by: spoonybard | last post by:
Hi Everyone, Recently, when I was trying to wire up an event for a GridView inside an AJAX Accordion, I was doing the wiring up in the code behind. The event never fired. When I moved that...
0
6904
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
7032
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
6873
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...
1
4767
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...
0
2990
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
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 ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
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.