I have the following problem: I have a list of UserControls for a wizard application and need to call a child method
List<UserControl> steps = new List<UserControl>();
steps.Add(new Step1());
steps.Add(new Step2());
steps.Add(new Step3());
steps.Add(new Step4());
All have StopTimeOut() method. How can I call: steps[0].StopTimeOut();
?
Thank you.
You should put the method in a common base class (eg, StepControl
) and make all four controls inherit from it.
You can then make a List<StepControl>
and call the function directly.
Well, you already did it:
steps[0].StopTimeOut();
Just declare in base class of all Step classes StopTimeOut
method as protected
or public
Example:
public Step : UserControl {
....
public virtual void StopTimeOut() {
//BASE IMPLEMENTATION
}
}
public Step1 : Step {
public override void StopTimeOut() {
//CHILD IMPLEMENTATION
}
}
public Step2 : Step {
public override void StopTimeOut() {
//CHILD IMPLEMENTATION
}
}
..
and in code:
List<Step> steps = new List<Step>();
steps.Add(new Step1());
steps.Add(new Step2());
..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With