Re: Using Event add/remove accessors
Christopher Frazier <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
This is perfectly legal and is referenced in CLR via C# (Richter) as well.
One thing you may want to do tho is implement a lock per event exposed to
make the calls thread safe:
public class AppInterface
{
private object _someEventLock = new Object();
public event EventHandler SomeEvent
{
add {
lock(_someEventLock){
HiddenAppClass.TheEvent += value;
}
}
remove {
lock(_someEventLock){
HiddenAppClass.TheEvent -= value;
}
}
}
}
--
-Christopher
| AspInsider
http://chrisfrazier.net/blog
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into your
(")_(") signature to help him gain world domination.
-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps and
controls [mailto:[email protected]] On Behalf Of
Bhatnagar, Amit
Sent: Wednesday, May 23, 2007 1:33 PM
To: [email protected]
Subject: [DOTNET-WINFORMS] Using Event add/remove accessors
Hi all.
In my program, there is a clear seperation between GUI code and application
code - this is implemented by the GUI only being able to "see" an interface
class (and specifically implementing the Command and Façade design
patterns.)
/
GUI -> \ -> AppInterface -> HiddenApp Class(es)
/
[Command] [Façade]
An object in the GUI (MyGuiObject) needs to receive an event from an object
in the app (HiddenAppObject), but I'd like to hide exactly where the event
comes from. So I implemented my own add/remove event accessors on the
AppInterface that does event registration/deregistration to the
HiddenAppClass:
public class AppInterface
{
public event EventHandler SomeEvent
{
add {HiddenAppClass.TheEvent += value);
remove {HiddenAppClass.TheEvent -= value);
}
}
so in the GUI, I only know of the AppInterface, but I am able to receive
events from the hidden application classes.
This works, but I am wondering if anyone knows a reason not to do such a
thing? I got this idea from
http://msdn.microsoft.com/msdnmag/issues/06/11/NETMatters/default.aspx.
thanks,
The information contained in this e-mail message is PRIVATE. It may contain
confidential information and may be legally privileged. It is intended for
the exclusive use of the addressee(s). If you are not the intended
recipient, you are hereby notified that any dissemination, distribution or
reproduction of this communication is strictly prohibited. If the intended
recipient(s) cannot be reached or if a transmission problem has occurred,
please notify the sender immediately by return e-mail and destroy all copies
of this message.
Thank you.