Need help with custom events.... please help

  • Thread starter Russell Smallwood
  • Start date
R

Russell Smallwood

Hey all,

Okay -

I'm trying to get a handle on setting up a custom event, as you can
probably
tell by the code below, I'm struggling.

Some background. What I'm trying to do is update the text in an rtf
control on my main form whenever something is written to the logfile.
Since I'm writing
to the log all over my application, I want to keep this rtf control and
the events that publish their verbage to the log loosely coupled. This
sounded like
the perfect application of the event model in the .NET framework.

As a sidebar, It sounds as if events and delegates are the ideal way to
pass information around your application. In other words, If the user
is sitting on
a form, then does something that calls another class, it seems that an
event/subscriber conversation is the perfect way to pass data back to
the main form.
Am I thinking correctly?

Anyway, back to my question ----


Somewhere, deep within the bowls of my Windows forms application, I have
this:

public class loggerEventArgs : EventArgs
{
public readonly string logData;
public readonly string logCaller;

public loggerEventArgs (string logString, string caller)
{
this.logCaller=caller;
this.logData=logString;
}


}
public class writeLog
{
private string _logData;
private string _logCaller;

public delegate void logHandler(object logstuff,
loggerEventArgs theseArgs);
public event logHandler OnlogWrite;

public writeLog(string logData, string caller)
{
this._logData=logData;
this._logCaller=caller;
}
public void publish()
{
loggerEventArgs thislogevent = new
loggerEventArgs(this._logData,this._logCaller);
if(OnlogWrite != null)
{
OnlogWrite(this,thislogevent);
}
}
}


Elsewhere in another class

writeLog mylog=new writeLog("Doing some stuff","stuff doer");
mylog.publish();

So far so good (I think). However, now how to I subscribe in my form
class?

I'm doing some stuff in my form's code that starts this whole process
off in the first place..
In other words, on my main form, I have a rich text control that I'm
using to display the stuff I'm
writing to my log, so I want to subscribe to the event somewhere in my
form's code.

This is what I would have expected to do:

writeLog.OnlogWrite += new writeLog.logHandler(updateLogWindow);

for which I would write a method called updateLogWindow which updated my
Rich Text control but this doesn't work.

Why? It appears as though I need a reference to the writeLog object,
but that doesn't make sense to me.

help.


-r

---------------------





--
Russell Smallwood
Relatia Software Corporation

Finally! Time and Billing in GoldMine
www.relatia.net
 
G

Guest

The way it works is like this: class1 has a member of type class2. Class2 can raise an event event1. Class1 subscribes to the class2 member event1 event. Therefore you do have a reference to the object that raises the event. If you don't want that, consider creating a static event, even though I have to admit I never tried using static events...

Comunicating through events is good but sometimes a direct connection might be better. Consider class1 having a member of type class2 and class2 having a member of type class1. You must be careful how you instantiate them. After that, you don't need events anymore, you simple refer each object from the other object. A typical case is when you have a list of objects of a certain type, and the objects themselves have a memeber that is a reference to the list. So each element can access the list and the list can access each element, without the need for events..
 

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

Top