Hello,
I am trying to create a public void delegate
GenericEventHandler in the event publishing class with a generic
parameter. The parameter is of a variable type supplied by the referenced Class
Library. The parameter/argument will be passed to the subcriber class
when the event is fired. The variable types library assembly is referenced in
both the publisher and subscriber classes. Could you please expalin how
to implement this functionality.
Thank you,
Serge
Generic Delegate
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Matthew CochranRank #252 · 10K reputationPosted Apr 17, 2008, 3:31 PM EDT
you can create a custom event args class like this:
public class EventArgs : EventArgs
{
public EventArgs(T value)
{
m_value = value;
}
private T m_value;
public T Value
{
get { return m_value; }
}
}
and then use it to pass your value around like with the sample classes:
public class Broadcaster
{
private event EventHandler<EventArgs<String>> m_handler;
public event EventHandler<EventArgs<String>> Triggered
{
add{ m_handler += value;}
remove{ m_handler -= value;}
}
public void FireEvent()
{
if(null != m_handler)
m_handler(this, new EventArgs<string>("My Value");
}
}
public class Listener
{
public void OnEvent(Object sender, EventArgs<String> args)
{
string val = args.Value;
// do something with value here;
}
}
I hope this helps
-Matt
Sergei Vasilyeu3 reputationPosted Apr 18, 2008, 11:58 AM EDT
regards,
serge