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>