none
Switch Case Problem RRS feed

  • Question

  •  This is my code block. but I have a small problem.

    IList<FrameworkElement> tControls = pControls.Cast<FrameworkElement>().ToList();

                    switch (tControls)
                    {
                        case "uc_AndOperator": // wrong part?? 

                               var uc_AndOperator = tControls.OfType<UcAndOperator>();
                                foreach (UcAndOperator tItem in uc_AndOperator)
                                {                             
                                    tItem.XMLAndOperatorWriter(tXWriter);                                                                                      
                                }    

                            break;
                     }

    tControls is a List. This List is holding User Controls. I want to if my UserControl type equal to case condition and run my function.

    This is my UserControls Types.

      I get this error. How can equal my case condition to frameworkElement.              

    • Edited by Burakhan Bircan Wednesday, June 12, 2019 11:52 AM wrong grammar
    Wednesday, June 12, 2019 11:52 AM

Answers

  • I think that you can use the uc_AndOperator variable and the foreach loop without switch. Use other types in OfType<…> to extract other controls.

    If you want to identify each type inside a loop, then consider this modern form of switch:

    foreach( var fe in tControls)

    {

       switch( fe)

       {

       case UcButtonType button:

             // process button . . .

          break;

       case UcAndOperator andOperator:

             // process And operator:

             andOperator.XMLAndOperatorWriter(tXWriter); 

          break;

       }

    }


    Wednesday, June 12, 2019 2:42 PM