Switch case statement error: A constant value is expected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mnarewec
    New Member
    • Nov 2007
    • 22

    Switch case statement error: A constant value is expected

    Hi all
    I have a TreeView in a MDI Apps. When the Nodes is selected it displays a MDI child form.

    I want to use a switch statement to determine which node is selected and to load or show the correct MDI child form.

    Here is part of the code:

    private void treeViewMDI_Aft erSelected (object sender, TreeViewEvenArg s e)
    {
    frmAB frmAB1 = new frmAB ();
    frmUDC frmUDC1 = new frmUDC();

    switch(e.Node.T ext)
    {
    case frmAB1.Text: // This did not compile
    frmAB1.MDIParen t=this;
    frmAB1.show();
    break;

    case frmUDC1.Text: //This did not compile
    frmUDC1.MDIPare nt=this;
    frmUDC1.show();
    break;
    }
    }


    The line of code that did not compile is stated above.

    Please assist or point me to right direction to solve this problem.

    Marsh
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by mnarewec
    Hi all
    I have a TreeView in a MDI Apps. When the Nodes is selected it displays a MDI child form.

    I want to use a switch statement to determine which node is selected and to load or show the correct MDI child form.

    Here is part of the code:

    private void treeViewMDI_Aft erSelected (object sender, TreeViewEvenArg s e)
    {
    frmAB frmAB1 = new frmAB ();
    frmUDC frmUDC1 = new frmUDC();

    switch(e.Node.T ext)
    {
    case frmAB1.Text: // This did not compile
    frmAB1.MDIParen t=this;
    frmAB1.show();
    break;

    case frmUDC1.Text: //This did not compile
    frmUDC1.MDIPare nt=this;
    frmUDC1.show();
    break;
    }
    }


    The line of code that did not compile is stated above.

    Please assist or point me to right direction to solve this problem.

    Marsh
    Like the error message says, you have to use a constant value for your cases. This is to ensure that the compiler can enforce the rule which says that "no two case constants within the same switch". If you use variables then the there is a possibility of breaking the rule.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by r035198x
      Like the error message says, you have to use a constant value for your cases. This is to ensure that the compiler can enforce the rule which says that "no two case constants within the same switch". If you use variables then the there is a possibility of breaking the rule.
      Hmm...doesn't look like you have this ability in C#. However, in VB we can do:

      Code:
      Select Case SomeObjectParameter 
        Case Is = SomeSecondObject.Parameter
      	'Do Stuff
        Case Is = SomeThirdObject.Parameter
      	'Do Stuff
        Case Else
      	'Do something else
      End Select

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by balabaster
        Hmm...doesn't look like you have this ability in C#. However, in VB we can do:

        Code:
        Select Case SomeObjectParameter 
          Case Is = SomeSecondObject.Parameter
        	'Do Stuff
          Case Is = SomeThirdObject.Parameter
        	'Do Stuff
          Case Else
        	'Do something else
        End Select
        VB's case is closer to C#'s if else than to the switch.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Oh VB nad your butchering of standard coding concepts.
          That is not a switch/case statement

          Comment

          Working...