Raising multiple events?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there. I'm referring to

http://msdn.microsoft.com/library/d...s/cpguide/html/cpconraisingmultipleevents.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
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top