Event handler Maddness
-
I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this:
Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc...
Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty:Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed);
I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance- -
I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this:
Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc...
Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty:Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed);
I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-Of course you get an error when the collection is empty, but that has nothing to do with event handlers. You're trying to access
Car
number 5 when there is noCar
number 5 in the collection if it's empty. An event is usually bound to an instance of an object (there are static events, too, but they're far less common) and unless you have the instance you can't assign event handlers to it.Regards, mav -- Black holes are the places where god divided by 0...
-
I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this:
Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc...
Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty:Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed);
I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-Hi Well if you have a tree then you can enumarate it and hook up the event handlers: Something like this:
foreach(Train train in this.trains) foreach(Car car in train.Cars) car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed);
if you are building the tree dynamically you can add the event handler on the fly for exampleCar car=new Car(); car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed); this.SelectedTrain.AddCar(car);
or//Somewhere in your domain public Car AddNewCarToTrain(Train train) { Car car=new Car(); //other initilizaing and validating code train.Cars.Add(car); return car; } //Then you can add your handler in your UI AddNewCarToTrain(this.selectedTrain).CarsChanged+=new CarsChangedEventHandler(Cars_Changed);
Regards -
I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this:
Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc...
Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty:Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed);
I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-Hi, Try using a index property in your objects (like Tag property for labels). And then set the same event handler for all you Cars ojects (for example) and depending on the index you can do your work. In your event handler you have a sender object. You can use something like this:
private void Cars_Changed(object sender,EventArgs e) { int index = ((Cars)object).index; }
Hope it helpsDo your best to be the best
-
Hi, Try using a index property in your objects (like Tag property for labels). And then set the same event handler for all you Cars ojects (for example) and depending on the index you can do your work. In your event handler you have a sender object. You can use something like this:
private void Cars_Changed(object sender,EventArgs e) { int index = ((Cars)object).index; }
Hope it helpsDo your best to be the best
This is what I wanted to do, but I need a little clarification. It looks like you are saying that all Car objects can fire the same event, and the eventArgs will include an index so I will know which car raised the event. If that is correct, then how do I declare the same event in each Car?? Thanks Squeaker
-
Hi Well if you have a tree then you can enumarate it and hook up the event handlers: Something like this:
foreach(Train train in this.trains) foreach(Car car in train.Cars) car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed);
if you are building the tree dynamically you can add the event handler on the fly for exampleCar car=new Car(); car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed); this.SelectedTrain.AddCar(car);
or//Somewhere in your domain public Car AddNewCarToTrain(Train train) { Car car=new Car(); //other initilizaing and validating code train.Cars.Add(car); return car; } //Then you can add your handler in your UI AddNewCarToTrain(this.selectedTrain).CarsChanged+=new CarsChangedEventHandler(Cars_Changed);
Regards -
This is what I wanted to do, but I need a little clarification. It looks like you are saying that all Car objects can fire the same event, and the eventArgs will include an index so I will know which car raised the event. If that is correct, then how do I declare the same event in each Car?? Thanks Squeaker
Hi, Try using this code:
private void Start() { Cars []myCars = new Cars[10]; for (int i = 0 ; i < myCars.Length ; i++) { //some other code myCars[i].Index = i; myCars[i].Click += new EventHandler(Cars_Click); } } private void Cars_Click(object sender,EventArgs e) { int index = ((Cars)sender).Index; }
Hope this answers to your questionDo your best to be the best