tags:

views:

49

answers:

3

In my current situation I have a class that performs an operation synchronously. During that operation an certain event will the thrown several times, depending on how often the situation where the event is raised occurs.
I know how the event-mechanism works and what the recommended ways of using are. Works well.(Note: My application is single-threaded)

What I want to is, to set a value to a property in my EventArgs. I've never had the need to set properties there.

This is a simplified situation of my current situation: (Note, I don't need answers telling me to use regex, or stringreplace, because that won't work in this situation)

I have this EventArgs:

public class TestEventArgs : EventArgs
{

    public String OldString { get; private set; }
    public String NewString { get; set; }

    public TestEventArgs(String oldString)
    {
        this.OldString = oldString;
    }
}

I do normally raise events this way:

public event EventHandler<TestEventArgs> ChangeString;

protected virtual void OnChangeString(String oldString)
{
    EventHandler<TestEventArgs> handler = this.ChangeString;
    if (handler != null)
    {
        handler(this, new TestEventArgs(oldString));
    }
}

and by calling the OnChangeString method, because I read that this in the way to raise events a long time ago.

Modified code where I need a value of the EventArgs after it's been raised:

public event EventHandler<TestEventArgs> ChangeString;

protected virtual void OnChangeString(TestEventArgs args)
{
    EventHandler<TestEventArgs> handler = this.ChangeString;
    if (handler != null)
    {
        handler(this, args);
    }
}
    public void Foo()
    {
        String oldString = "this is the old string";
        // this.OnChangeString(oldString) // this is the way I called before
        // now I need to keep a reference to the EventArgs
        TestEventArgs args = new TestEventArgs(oldString);
        this.OnChangeString(args);
        // here I do have full access to args.NewString
    }

So, is it ok to keep a reference for the EventArgs and to raise the event with a method that accepts my EventArgs as parameter?

+1  A: 

I think your question boils down to:

Is it OK to use the the arguments of an event to communicate data back to the source of the event?

First and foremost: at the low level, this will work. You can modify the event object, and you can see those changes in the code that raises the event.

But what if there are multiple event listeners. Who gets to set NewString?

If you expect only one, it is more clear to pass a delegate (of type Func<String, String>) to the class that now raises the event.

jdv
The event will only be subscribed at one specific place. But I do understand the problem if its subscribed at multiple places. I'll stick with a delegate for my situation.
citronas
+1  A: 

If I get your question right, you generally don't want to keep a reference to your TestEventArg, and reuse this object on another event.

In your case, your TestEventArg has a public properties call NewString. If any event handler updates this property, while it is begin reference by multiple part of your program, you may accidentally running into problem where the NewString property kept changing from another part of your problem.

However, if your TestEventArg has only readable property, then I see no problem keeping the reference and reuse it.

public class TestEventArgs : EventArgs
{

    public String OldString { get; private set; }
    public String NewString { get; private set; }

    public TestEventArgs(String oldString, String newString)
    {
        this.OldString = oldString;
        this.NewString - newString
    }
}

In general, you want to keep your event arg immutable which will makes your program easier understand and debug.

dsum
+1  A: 

Only time I keep the TestEventArg arg (in this case) is if you need access to the variable after the method has been called. Example of this is the OnClosing event for a Form. It has a variable you can set to tell it to cancel the close. In this case, if the client registers the callback, and can set a property on the TestEventArg class, then you'd want to have a variable pointed to it. If all you want to do is pass information to the callback and don't need anything back, then the first method is what I use.

One caveat for me is that if I use the OnChangeString method, I write it so that it encapsulates the details of the event args.

protected virtual void OnChangeString(string oldString)
{
    EventHandler<TestEventArgs> handler = this.ChangeString;
    if (handler != null)
    {
        var args = new TestEventArgs(oldString);
        handler(this, args);

        // Do something with args
    }
}
Bryce Fischer
System.Web.UI.WebControls.ServerValidateEventArgs does something similiar. In my case I'd need to use args outside of this function. So its best as jdv said to use in delegate in my concrete case
citronas