Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2005

Tip: Looking for answers? Try searching our database.

How are CLR events defined?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
asylumn - 10 Nov 2005 19:58 GMT
I'm having a srange issue with events in my app. I am trying to embed Lua
scriping using LuaInterface ( http://luaforge.net/projects/luainterface/ ),
which basically allows Lua scripts to access CLR object, including their
events.

The problem I am having is that it will only work with events from the CLR,
I cannot get it to work with events from my own classes, leading me to think
that events inside of the CLR are being declared differently then the way I
am doing it. Does anyone know how events are declared in, for example,
System.Windows.Forms.Control?   If I use LuaInterface with a Form it works
perfectly and I can subscribe to events, I just can't get it to work with my
own events.

For my own classes, I use something like the following:

public class TestClass
{
  public TestClass()
  {
  }

  protected virtual void OnTestEvent (EventArgs e)
  {
     if ( TestEvent != null )
        TestEvent (null, e);
  }

  public event EventHandler TestEvent;
}

But that doesn't work, when the LuaInterface reflects over this class
TestEvent is seen as a field, not an event, so it doesn't add the requisite
methods for me to use it.   If I do the same thing with a CLR class, Form
for example, when it reflects over it OnMouseDown is seen as an event, not a
field.  I don't know why.

Thanks
Jon Shemitz - 10 Nov 2005 21:06 GMT
> The problem I am having is that it will only work with events from the CLR,

>    protected virtual void OnTestEvent (EventArgs e)
>    {
[quoted text clipped - 3 lines]
>
>    public event EventHandler TestEvent;

There are two different ways to use "event." The way you use it
creates a field: the only real difference between the event "event
EventHandler TestEvent" and the delegate "EventHandler TestEvent" is
that the event can only be a field - never a local.

The other way is to declare a private event field, and a public event
with add/remove handlers.

 protected virtual void OnTestEvent (EventArgs e)
 {
   if ( _TestEvent != null )
     _TestEvent (null, e);
 }

 private event EventHandler _TestEvent;
 public event EventHandler TestEvent
 {
   add    { _TestEvent += value; }        
   remove { _TestEvent -= value; }        
 }

External code can only subscribe/unsubcribe to TestEvent; only your
class can actually fire the private _TestEvent.

I don't know Lua, but I'd bet that this second type of event is what
it's looking for.

Signature

    <http://www.midnightbeach.com>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.