Creating an Event and passing an object?  
Author Message
CL0CKW0RK





PostPosted: Visual C# General, Creating an Event and passing an object? Top

I don't really know C#, I work in Visual basic. However I am trying to add some code to a dll written in C#. I would like to raise an event and send an object with it... not sure if thats the right terminology or not.

while ((msg = Server.ReadMessage()) != null)
{
event_raising_code(msg); //help
}

This way in Visual basic I can setup an event handler to run things when i get a message, and at the same time get the msg object over into visual basic to dissect.
I've been trying to figure this out all day and still am unable to properly write the event, I just don't know enough about C#. Help


Visual C#20  
 
 
SvenC





PostPosted: Visual C# General, Creating an Event and passing an object? Top

You have to set up a delegate which is basically a declaration of the function prototype for your "event_raising_code" function. Then you define an event of that delegate type so that your caller can add a handler for that event. Like so:

public class YourCSharpClass
{
public delegate void ProcessMessage(YourMessageType msg);
public event ProcessMessage OnNewMessage;

public void DoIt()
{
YourMessageType msg = null;
while((msg = Server.ReadMessage()) != null)
{
if(OnNewMessage != null)
OnNewMessage(msg);
}
}
}

In your VB code you just use withevents or AddHandler to subscribe to the Event OnNewMessage with a sub that has one parameter of type YourMessageType.

--
SvenC


 
 
CL0CKW0RK





PostPosted: Visual C# General, Creating an Event and passing an object? Top

much thanks, trying it out.