471,337 Members | 931 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Raising multiple events?

Hi there. I'm referring to

http://msdn.microsoft.com/library/de...ipleevents.asp

The article implies that rather than having one field backing each event
that your class has, you can use this alternate method if you have many
fields in your class. However, the example has 2 events and has two objects
backing those events where the delegates are stored for those events. How is
this a savings? Is it talking about the case where many delegates
representing methods are later hooked into the event? Because the class
developer wouldn't have knowledge of that. Also, what is the following:
protected Delegate GetEventHandler(object key) {...}

What code goes in there? what is the "key?"

Thanks for any help...

-Ben
Feb 23 '06 #1
3 1548
Hi Ben,

The key point is the object key is static.
// Defines a unique key for each event.
static readonly object mouseDownEventKey = new object();
static readonly object mouseUpEventKey = new object();

Notice that if we have a textbox and button derived from control.
When we instance the two class, but in the memory there will be one copy of
the mouseDownEventKey and mouseUpEventKey. But the mouseDownEventKey is
static.
Because there will be a lot of controls including the Form itself, we will
save a lot of memory.

Also once the mouseDownEventKey is defined, it will keep for all the
controls that inherits from control. That is why it is declared as readonly.
As for the method below.
protected Delegate GetEventHandler(object key) {...}
Simply, it is just a HashTable function, we can implement it on our own.
e.g.
//define the storage in hashtable
HashTable ht = new HashTable();
protected Delegate GetEventHandler(object key)
{
return ht[key];
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 24 '06 #2
Thanks for the explanation. Very helpful. Incidentally, I found an updated
version of the article with more detail at:

http://msdn2.microsoft.com/en-us/library/8843a9ch.aspx

Note that the code block does not render properly in IE7 Beta 2.

Thanks for your help, Peter...

-Ben

""Peter Huang" [MSFT]" wrote:
Hi Ben,

The key point is the object key is static.
// Defines a unique key for each event.
static readonly object mouseDownEventKey = new object();
static readonly object mouseUpEventKey = new object();

Notice that if we have a textbox and button derived from control.
When we instance the two class, but in the memory there will be one copy of
the mouseDownEventKey and mouseUpEventKey. But the mouseDownEventKey is
static.
Because there will be a lot of controls including the Form itself, we will
save a lot of memory.

Also once the mouseDownEventKey is defined, it will keep for all the
controls that inherits from control. That is why it is declared as readonly.
As for the method below.
protected Delegate GetEventHandler(object key) {...}
Simply, it is just a HashTable function, we can implement it on our own.
e.g.
//define the storage in hashtable
HashTable ht = new HashTable();
protected Delegate GetEventHandler(object key)
{
return ht[key];
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 27 '06 #3
Hi Ben,

Thanks for your quickly reply and information sharing!
You are welcomed!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by serge calderara | last post: by
reply views Thread by Joe Campbell | last post: by
4 posts views Thread by Dave A | last post: by
reply views Thread by Apu Nahasapeemapetilon | last post: by
3 posts views Thread by =?Utf-8?B?c21rcmFtZXIwNzJmZGFzZmFzZGY=?= | last post: by
reply views Thread by rosydwin | last post: by

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.