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
Loading
Sergei VasilyeuPosted Apr 18, 2008, 3:58 PM
regards,
serge
Matthew CochranPosted Apr 17, 2008, 7:31 PM
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