C # calling child method from parent list

I have the following problem: I have a list of UserControls for the wizard application and I need to call the child method

List<UserControl> steps = new List<UserControl>(); steps.Add(new Step1()); steps.Add(new Step2()); steps.Add(new Step3()); steps.Add(new Step4()); 

Everyone has a StopTimeOut () method. How can I call: steps[0].StopTimeOut(); ?

Thanks.

+4
source share
2 answers

Well, you already did it:

 steps[0].StopTimeOut(); 

Just declare all Step StopTimeOut classes in the base class 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()); .. 
+4
source

You must put the method in a common base class (e.g. StepControl ) and make all four controls inherit from it.

Then you can make a List<StepControl> and call the function directly.

+6
source

Source: https://habr.com/ru/post/1493043/


All Articles