I’m new to event programming, and I’m obviously misunderstanding something that I’m trying to do.
I have a Windows Forms application that subscribes to events from another class. T
//Class that provides event handler to Windows Forms application.
class Foo
{
public string Value{get; set;}
// Lots of other code
public void OnEventFired(object sender, EventArgs e)
{
// Attempt to access variable Value here.
}
}
From the Windows Form code I’m first setting the variable Value in class Foo before triggering the event that will execute the code in OnEventFired above.
What I’m seeing is that when used in the event handler the variable Value doesn’t contain the value that was set before the event was fired (Value is null).
I know I can extend EventArgs to include the variable data, but I’m trying to understand why what I’m doing doesn’t work.
Here’s a short example which works. Compare this to your code to work out what’s wrong.
My guess is that you’ve hooked up the event handler using a different instance of
Foothan the one you’ve setValuein. For example, like this:… but it’s hard to tell without seeing any more code.