c# - Class variable availablility after event is fired -
i'm new event programming, , i'm misunderstanding i'm trying do.
i have windows forms application subscribes events class. t
//class provides event handler windows forms application. class foo { public string value{get; set;} // lots of other code public void oneventfired(object sender, eventargs e) { // attempt access variable value here. } }
from windows form code i'm first setting variable value
in class foo
before triggering event execute code in oneventfired
above.
what i'm seeing when used in event handler variable value
doesn't contain value set before event fired (value
null).
i know can extend eventargs
include variable data, i'm trying understand why i'm doing doesn't work.
here's short example works. compare code work out what's wrong.
using system; using system.windows.forms; class foo { public string value { get; set; } public void handleclick(object sender, eventargs e) { ((control)sender).text = value; } } class program { public static void main() { foo foo = new foo { value = "done" }; button button = new button { text = "click me!" }; button.click += foo.handleclick; form form = new form { controls = { button } }; application.run(form); } }
my guess you've hooked event handler using different instance of foo
1 you've set value
in. example, this:
foo foo = new foo { value = "done" }; ... // different instance of foo! button.click += new foo().handleclick;
... it's hard tell without seeing more code.
Comments
Post a Comment