J
John Salerno
My general problem with events seems to be that there are so many parts
to them, and I'm not sure where they all go or when you use which parts.
For example, there's the delegate, the event name, the event handler,
then the coding that wires it together. For the most part, I do
understand these things, but I get confused when it comes to putting it
all together.
More specifically, here's some code from a book:
this.txtMonthlyInvestment.TextChanged += new
System.EventHandler(this.txtMonthlyInvestment_TextChanged);
First off, do you need the 'this' keyword? Second, where does this code
go? In the Load event of the form that contains the text box?
Another example:
ProductList products = new ProductList(); //an array list object
products.Changed += new EventHandler(ChangedHandler);
Why isn't 'this' used in the second example? Is the book being
inconsistent, or is it a different situation? (In the book, it says it's
calling it from a different class, but I don't know what that means
exactly: "To handle an event from another class, you create an instance
of the class that raises the event and assign it to a class variable.")
Where does that first line go? (The declaration of products). The above
two lines are the entire example (except for the event handler itself),
so I think some stuff has been left out. It doesn't show which class
it's included in.
Basically I'm just very lost when it comes to events, and I always have.
I don't understand where all the code goes for each 'part', and that's
probably what it all comes down to, I guess.
Any help would be appreciated, or any suggestions for sites that clearly
explain events.
Thanks,
John
to them, and I'm not sure where they all go or when you use which parts.
For example, there's the delegate, the event name, the event handler,
then the coding that wires it together. For the most part, I do
understand these things, but I get confused when it comes to putting it
all together.
More specifically, here's some code from a book:
this.txtMonthlyInvestment.TextChanged += new
System.EventHandler(this.txtMonthlyInvestment_TextChanged);
First off, do you need the 'this' keyword? Second, where does this code
go? In the Load event of the form that contains the text box?
Another example:
ProductList products = new ProductList(); //an array list object
products.Changed += new EventHandler(ChangedHandler);
Why isn't 'this' used in the second example? Is the book being
inconsistent, or is it a different situation? (In the book, it says it's
calling it from a different class, but I don't know what that means
exactly: "To handle an event from another class, you create an instance
of the class that raises the event and assign it to a class variable.")
Where does that first line go? (The declaration of products). The above
two lines are the entire example (except for the event handler itself),
so I think some stuff has been left out. It doesn't show which class
it's included in.
Basically I'm just very lost when it comes to events, and I always have.
I don't understand where all the code goes for each 'part', and that's
probably what it all comes down to, I guess.
Any help would be appreciated, or any suggestions for sites that clearly
explain events.
Thanks,
John