473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

It it possible to redirect events?

class Manager
{
public event ItemEventHandler ItHappened;
public Manager
{
Item i;
i.ItHappend+=new ItemEventHandler(OnItHappened);
}
void OnItHappened(...)
{
this.ItHappened();
}
}
class Item
{
public event ItemEventHandler ItHapped;
}

Above are simplified version of two classes. The client interacts with
the Manager, and the Manager has many 'Items'. This scheme works but
looks kind of a waste and overhead, because everytime an item fires
ItHapped, OnItHappened is called which simply fires the Manager's the
same event with the same parameters. I just wondered, if this thing
would be possible
class Client
{
void Init()
{
Manager m;
m.ItHappend += new ItemEventHandler(...);
~~~~~~~~~~~~~~~~~~~~~~ Let's call this thing
X
}
}
class Manager
{
public event ItemEventHandler ItHappened;
public Manager
{
Item i;
i.ItHappend+=somehow get X from Manager's ItHappend handlers;
}
}

If this is possible, then there's no need to call the trivial method
each time. Is it possible to get the handler list of the Manager's
ItHappened event at that point?
Thanks.
Dec 10 '07 #1
5 4380
Sin Jeong-hun,

You could call the GetInvocationList method on the delegate and that
will give you an array of individual delegates that you could the attach
(one-by-one) to the ItHappened event on the Item class.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:49**********************************@s8g2000p rg.googlegroups.com...
class Manager
{
public event ItemEventHandler ItHappened;
public Manager
{
Item i;
i.ItHappend+=new ItemEventHandler(OnItHappened);
}
void OnItHappened(...)
{
this.ItHappened();
}
}
class Item
{
public event ItemEventHandler ItHapped;
}

Above are simplified version of two classes. The client interacts with
the Manager, and the Manager has many 'Items'. This scheme works but
looks kind of a waste and overhead, because everytime an item fires
ItHapped, OnItHappened is called which simply fires the Manager's the
same event with the same parameters. I just wondered, if this thing
would be possible
class Client
{
void Init()
{
Manager m;
m.ItHappend += new ItemEventHandler(...);
~~~~~~~~~~~~~~~~~~~~~~ Let's call this thing
X
}
}
class Manager
{
public event ItemEventHandler ItHappened;
public Manager
{
Item i;
i.ItHappend+=somehow get X from Manager's ItHappend handlers;
}
}

If this is possible, then there's no need to call the trivial method
each time. Is it possible to get the handler list of the Manager's
ItHappened event at that point?
Thanks.

Dec 10 '07 #2
On Mon, 10 Dec 2007 11:31:48 -0800, Sin Jeong-hun <ty*******@gmail.com
wrote:
[...]
Above are simplified version of two classes. The client interacts with
the Manager, and the Manager has many 'Items'. This scheme works but
looks kind of a waste and overhead, because everytime an item fires
ItHapped, OnItHappened is called which simply fires the Manager's the
same event with the same parameters. I just wondered, if this thing
would be possible
Surely it is. But your question is not very clear. For one, the code you
posted wouldn't compile (the Manager class constructor needs a "()" after
the name of the constructor), and even if it did, it wouldn't do anything
because the instance of Item isn't initialized, nor is it referenced
anywhere other than in a local variable. Even if you did subscribe to it,
it doesn't stick around long enough to do anything, at least not as you've
written it here.

Without correct code, it's hard to know for sure what behavior it is you
actually want, since there's no behavior actually being described
unambiguously.

So, I'll make some assumptions about what you want, but if they are wrong
you'll have to elaborate to clear up the misunderstanding.

I'm going to assume that what you want is an event in the Manager class
that, when subscribed to, essentially subscribes to the event in the Item
class. I don't really think that forwarding the event is really all that
big of a problem. But assuming you really want to get rid of the
forwarding, you could implement your event explicitly in the Manager
class, subscribing to the underlying Item event. For example:

class Manager
{
private Item _item = new Item();

public event ItemEventHandler ItHappened
{
add { _item.ItHappened += value; }
remove { _item.ItHappened -= value; }
}
}

That way anyone who tries to subscribe to the Manager event really winds
up subscribing to the Item event.

Note that this actually changes the reference relationships between your
instances. Objects subscribed to the Manager class event now wind up
referenced by the instance of Item, which means that you could release the
Manager instance without releasing the subscribers to the event in
situations where they would have been had they subscribed directly to
something in the Manager class.

If the Item instance is referenced only by the Manager class, that's nota
problem. But if for some reason the Item instance has a lifetime
different from the Manager class, it could create some confusing behavior
for clients of the Manager class (specifically, where the client thought
they'd released the one thing referencing their own instances, when in
fact they hadn't).

I think this is a very important thing to be aware of and is a good reason
to just forward the events through a whole new event unless you have a
very good reason for doing it some other way. There shouldn't be a
significant performance overhead introducing an intermediate event in the
Manager class that forwards events raised by the Item class, and it's a
nice, simple way to do that.

Pete
Dec 10 '07 #3
On Dec 11, 5:00 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 10 Dec 2007 11:31:48 -0800, Sin Jeong-hun <typing...@gmail.com>
wrote:
[...]
Above are simplified version of two classes. The client interacts with
the Manager, and the Manager has many 'Items'. This scheme works but
looks kind of a waste and overhead, because everytime an item fires
ItHapped, OnItHappened is called which simply fires the Manager's the
same event with the same parameters. I just wondered, if this thing
would be possible

Surely it is. But your question is not very clear. For one, the code you
posted wouldn't compile (the Manager class constructor needs a "()" after
the name of the constructor), and even if it did, it wouldn't do anything
because the instance of Item isn't initialized, nor is it referenced
anywhere other than in a local variable. Even if you did subscribe to it,
it doesn't stick around long enough to do anything, at least not as you've
written it here.

Without correct code, it's hard to know for sure what behavior it is you
actually want, since there's no behavior actually being described
unambiguously.

So, I'll make some assumptions about what you want, but if they are wrong
you'll have to elaborate to clear up the misunderstanding.

I'm going to assume that what you want is an event in the Manager class
that, when subscribed to, essentially subscribes to the event in the Item
class. I don't really think that forwarding the event is really all that
big of a problem. But assuming you really want to get rid of the
forwarding, you could implement your event explicitly in the Manager
class, subscribing to the underlying Item event. For example:

class Manager
{
private Item _item = new Item();

public event ItemEventHandler ItHappened
{
add { _item.ItHappened += value; }
remove { _item.ItHappened -= value; }
}
}

That way anyone who tries to subscribe to the Manager event really winds
up subscribing to the Item event.

Note that this actually changes the reference relationships between your
instances. Objects subscribed to the Manager class event now wind up
referenced by the instance of Item, which means that you could release the
Manager instance without releasing the subscribers to the event in
situations where they would have been had they subscribed directly to
something in the Manager class.

If the Item instance is referenced only by the Manager class, that's not a
problem. But if for some reason the Item instance has a lifetime
different from the Manager class, it could create some confusing behavior
for clients of the Manager class (specifically, where the client thought
they'd released the one thing referencing their own instances, when in
fact they hadn't).

I think this is a very important thing to be aware of and is a good reason
to just forward the events through a whole new event unless you have a
very good reason for doing it some other way. There shouldn't be a
significant performance overhead introducing an intermediate event in the
Manager class that forwards events raised by the Item class, and it's a
nice, simple way to do that.

Pete
I really appreciate your time on my question. It is really a detailed
answer!
First of all, that codes in my original post were not realy codes.
Real codes
were long so I only depicted parts necessary to explain the question,
all names
of classes, methods or events are boguses. Sure it wouldn't compile.
What I was trying to say was like this.
1.Client doesn't directly interacts with Item because there are two
many Items.
2.Client intereacts with Manager
3.Item fires ItHappened events.
4.Client should respond to ItHappened events.
5.Since Client doesn't directly instantiate Items, Client subscribes
to the Managers ItHappened events.
6.Manager's ItHappened event is nothing but simply redirecting
ItHappened events from its many items.
7.According to my oroginal scheme, everytime an ItHappened event fired
from an Item, A method in the Manager is called, which simply fires
its ItHappened events, thus redirecting it to the Client.
8.In 7, calling the method each time seemed like an overhead.

Now, I'm going to read carefully what you've written. Thanks again.
Dec 11 '07 #4
On Dec 11, 4:46 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Sin Jeong-hun,

You could call the GetInvocationList method on the delegate and that
will give you an array of individual delegates that you could the attach
(one-by-one) to the ItHappened event on the Item class.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Sin Jeong-hun" <typing...@gmail.comwrote in message

news:49**********************************@s8g2000p rg.googlegroups.com...
class Manager
{
public event ItemEventHandler ItHappened;
public Manager
{
Item i;
i.ItHappend+=new ItemEventHandler(OnItHappened);
}
void OnItHappened(...)
{
this.ItHappened();
}
}
class Item
{
public event ItemEventHandler ItHapped;
}
Above are simplified version of two classes. The client interacts with
the Manager, and the Manager has many 'Items'. This scheme works but
looks kind of a waste and overhead, because everytime an item fires
ItHapped, OnItHappened is called which simply fires the Manager's the
same event with the same parameters. I just wondered, if this thing
would be possible
class Client
{
void Init()
{
Manager m;
m.ItHappend += new ItemEventHandler(...);
~~~~~~~~~~~~~~~~~~~~~~ Let's call this thing
X
}
}
class Manager
{
public event ItemEventHandler ItHappened;
public Manager
{
Item i;
i.ItHappend+=somehow get X from Manager's ItHappend handlers;
}
}
If this is possible, then there's no need to call the trivial method
each time. Is it possible to get the handler list of the Manager's
ItHappened event at that point?
Thanks.
Thank you for the simple and easy answer. I'll look into the method.
Dec 11 '07 #5
On Tue, 11 Dec 2007 12:56:22 -0800, Sin Jeong-hun <ty*******@gmail.com>
wrote:
[...]
7.According to my oroginal scheme, everytime an ItHappened event fired
from an Item, A method in the Manager is called, which simply fires
its ItHappened events, thus redirecting it to the Client.
8.In 7, calling the method each time seemed like an overhead.
I think that given all of that, you might as well stick with the existing
design, in which your Manager has its own event that clients subscribe to,
and which it raises when its own handler subscribed to the Item's event is
executed.

Nicholas and I have both provided alternatives, but I think that in either
of our suggestions, implementing the suggestion needlessly complicates the
implementation. What you've got right now is simple and maintainable and
has no excessive performance overhead.

I'm glad we could answer your question, but I think you didn't really need
us. :)

Pete
Dec 11 '07 #6

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

Similar topics

10
by: Bob Garbados | last post by:
forgive my ignorance, as I'm new to php coming from a ms background... If I create a page named redirect.php and it's only content is: <?php header("Location: http://www.google.com"); ?>...
3
by: Frank T. Clark | last post by:
How do I redirect or capture keydown events in a parent form from a child form? I have a main form which displays another informational form marked "SizableToolWindow". Form child = new...
3
by: Johnb41 | last post by:
My web page has a bunch of text boxes, check boxes, radio buttons, etc. The checkboxes have an OnCheckedChanged event. The textboxes have an OnTextChanged event. etc... When my page is...
4
by: Marty U. | last post by:
I have a Session variable I need to check the value of. If it is value a then redirect to some page. I need to implement this in a user control that is on all the relevent pages. I placed the if...
5
by: MN | last post by:
Hello all - I'm hoping someone will be able to share their knowledge with me about this issue I'm having because I have run out of answers. Here's the scenario.... I have a .aspx page that...
0
by: Steve Lutz | last post by:
I have some global prosessing done on both the PreRequestHandlerExecute and PostRequestHandlerExecute events in my Global.asax. These event handlers do some session management things and some...
3
by: michael_vanommeren | last post by:
I have two web applications that I am working with and I am trying to do a Response.Redirect from one to the other. They are setup as separate web applications on the same IIS server. If I go...
4
by: David Thielen | last post by:
Hi; The ASP .NET book I have only talks about putting everything in a single aspx and aspx.cs file. However, it seems to me you want to redirect to a different page/class for each logical page....
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app. When session is invalid, how to redirect user to the login page? I don't want to add the code to redirect user to the login page into every page. Thanks, -Billy
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.