Are EventArg classes needed now that we have generics
Asked Answered
T

4

19

With generics, is there ever a reason to create specific derived EventArg classes

It seems like now you can simply use them on the fly with a generic implementation.

Should i go thorugh all of my examples and remove my eventArg classes (StringEventArgs, MyFooEventArgs, etc . .)

public class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        m_value = value;
    }

    private T m_value;

    public T Value
    {
        get { return m_value; }
    }
}
Terrell answered 22/11, 2008 at 15:36 Comment(1)
I don't understand, what do you want to do with "EventArgs<T>" class. Generally use "EventArgs" class and if you need some specific arguments, derive from "EventArgs" and add required properties.Brendabrendan
F
38

What you are describing are essentially tuples, grouped values used for a particular purpose. They are a useful construct in functional programming and support that style very well.

The downside is that their values are not named, and they require context to be understood. EventArgs by their very nature are often consumed far away from their relevant context. Therefore, tuple-esque EventArgs can be very confusing for the consumer.

Let's say we have an event indicating some division has been completed, and it carries the numerator, denominator, and result:

public event EventHandler<EventArgs<double, double, double>> Divided;

The event handler has some ambiguity:

private void OnDivided(object sender, EventArgs<double, double, double> e)
{
    // I have to just "know" this - it is a convention

    var numerator = e.Value1;
    var denominator = e.Value2;
    var result = e.Value3;
}

This would be much clearer with an EventArgs representing the event:

private void OnDivided(object sender, DividedEventArgs e)
{
    var numerator = e.Numerator;
    var denominator = e.Denominator;
    var result = e.Result;
}

Generic reusable EventArgs classes ease development of the mechanism at the expense of expressing intent.

Fructify answered 22/11, 2008 at 18:25 Comment(2)
You're example can be implemented using a generic event args to return your own class as: public class Division { double Num, double Denom, double Result}, and then EventHandler<EventArgs<Division>>.Fernanda
For those using more recent C# 7 and beyond, you can now return named tuples, like: (bool Result, string Response, ErrorClass Error).Tsan
E
3

Look at the Custom Generic EventArgs article written by Matthew Cochran, in that article he describes how to expand it even further with two and three members.

Using generic EventArgs have their uses, and of course their misuses, as type information is lost in the process.

public class City {...}

public delegate void FireNuclearMissile(object sender, EventArgs<City> args);
public event FireNuclearMissile FireNuclearMissileEvent;

public delegate void QueryPopulation(object sender, EventArgs<City> args);
public event QueryPopulation QueryPopulationEvent;

In the following example it is type-safe, but a bit more LOC:

class City {...}

public class FireNuclearMissileEventArgs : EventArgs
{
    public FireNuclearMissileEventArgs(City city)
    {
        this.city = city;
    }

    private City city;

    public City City
    {
        get { return this.city; }
    }
}

public delegate void FireNuclearMissile(object sender, FireNuclearMissileEventArgs args);
public event FireNuclearMissile FireNuclearMissileEvent;

public class QueryPopulationEventArgs : EventArgs
{
    public QueryPopulationEventArgs(City city)
    {
        this.city = city;
    }

    private City city;

    public City City
    {
        get { return this.city; }
    }
}

public delegate void QueryPopulation(object sender, QueryPopulationEventArgs args);
public event QueryPopulation QueryPopulationEvent;
Embraceor answered 22/11, 2008 at 17:0 Comment(1)
Would you care to elaborate on "their uses, and of course their misuses"? See also my question: #15766719Nasia
W
1

I think Tuple-style EventArgs are useful. Just like Tuple's, they can be misused, but it seems my laziness is stronger than my sense of caution. I implemented the following:

public static class TupleEventArgs
{
    static public TupleEventArgs<T1> Create<T1>(T1 item1)
    {
        return new TupleEventArgs<T1>(item1);
    }

    static public TupleEventArgs<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
    {
        return new TupleEventArgs<T1, T2>(item1, item2);
    }

    static public TupleEventArgs<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
    {
        return new TupleEventArgs<T1, T2, T3>(item1, item2, item3);
    }
}

public class TupleEventArgs<T1> : EventArgs
{
    public T1 Item1;

    public TupleEventArgs(T1 item1)
    {
        Item1 = item1;
    }
}

public class TupleEventArgs<T1, T2> : EventArgs
{
    public T1 Item1;
    public T2 Item2;

    public TupleEventArgs(T1 item1, T2 item2)
    {
        Item1 = item1;
        Item2 = item2;
    }
}

public class TupleEventArgs<T1, T2, T3> : EventArgs
{
    public T1 Item1;
    public T2 Item2;
    public T3 Item3;

    public TupleEventArgs(T1 item1, T2 item2, T3 item3)
    {
        Item1 = item1;
        Item2 = item2;
        Item3 = item3;
    }
}

Can be used as follows (when used with an event raiser extension)

public event EventHandler<TupleEventArgs<string,string,string>> NewEvent;

NewEvent.Raise(this, TupleEventArgs.Create("1", "2", "3"));
Weinrich answered 1/8, 2013 at 18:58 Comment(0)
P
0

As TcKs already said: Use EventArgs<T> if you only need to pass one value, otherwise derive from EventArgs (or EventArgs<T>, whatever you want).

Persinger answered 22/11, 2008 at 15:43 Comment(1)
but if i have a generic implementation, i dont need to create specific classes. i can just use them . .Terrell

© 2022 - 2024 — McMap. All rights reserved.