Evaluate true in C# using switch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    Evaluate true in C# using switch

    Hey all, I'm hoping someone may be able to help me. I need to evaluate
    some strings to see if their values are what I expect and it doesn't
    seem to work. Any help you can provide would be really appreciated.
    Here is my non-working syntax:

    switch(true)
    {
    case Header.ParentSe ctionDisplayNam e == String.Empty:
    MyString = Header.SectionD isplayName;

    case Header.ParentSe ctionDisplayNam e != String.Empty &
    Header.Breadcru mb.ParentSectio nCategoryDispla yName ==
    String.Empty:
    HBXPageName = Header.SectionD isplayName;
    }

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Evaluate true in C# using switch

    Jim,

    You can not do this with C#. You will have to use if statements in
    order to branch out this logic.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Jim" <Jim.Cardamone@ gmail.com> wrote in message
    news:1112893351 .045883.143730@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > Hey all, I'm hoping someone may be able to help me. I need to evaluate
    > some strings to see if their values are what I expect and it doesn't
    > seem to work. Any help you can provide would be really appreciated.
    > Here is my non-working syntax:
    >
    > switch(true)
    > {
    > case Header.ParentSe ctionDisplayNam e == String.Empty:
    > MyString = Header.SectionD isplayName;
    >
    > case Header.ParentSe ctionDisplayNam e != String.Empty &
    > Header.Breadcru mb.ParentSectio nCategoryDispla yName ==
    > String.Empty:
    > HBXPageName = Header.SectionD isplayName;
    > }
    >[/color]


    Comment

    • Peter Rilling

      #3
      Re: Evaluate true in C# using switch

      You cannot use expression in switch statements, they must be constants.

      Use IF block.

      "Jim" <Jim.Cardamone@ gmail.com> wrote in message
      news:1112893351 .045883.143730@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > Hey all, I'm hoping someone may be able to help me. I need to evaluate
      > some strings to see if their values are what I expect and it doesn't
      > seem to work. Any help you can provide would be really appreciated.
      > Here is my non-working syntax:
      >
      > switch(true)
      > {
      > case Header.ParentSe ctionDisplayNam e == String.Empty:
      > MyString = Header.SectionD isplayName;
      >
      > case Header.ParentSe ctionDisplayNam e != String.Empty &
      > Header.Breadcru mb.ParentSectio nCategoryDispla yName ==
      > String.Empty:
      > HBXPageName = Header.SectionD isplayName;
      > }
      >[/color]


      Comment

      • Bruce Wood

        #4
        Re: Evaluate true in C# using switch

        Your way of writing switch statements looks more like a COBOL style,
        which is the reverse of the way C# does it. C# insists on:

        switch (expression)
        {
        case value1:
        case value2:
        ...
        }

        you have the value in the "switch" and the expressions in the "case"s.

        As well, C# insists that the expression and the values be simple types
        like integers and enums. Strings, floating point values, decimal
        values, and types you create yourself aren't allowed.

        To do what you want, you need to say:

        if (Header.ParentS ectionDisplayNa m­e == String.Empty)
        {
        MyString = Header.SectionD isplayName;
        }
        else if (Header.ParentS ectionDisplayNa m­e != String.Empty &&
        Header.Breadcru mb.ParentSectio ­nCategoryDispl ayName ==
        String.Empty)
        {
        HBXPageName = Header.SectionD isplayName;
        }

        etc.

        (Also take care to use && in boolean tests rather than &.)

        Comment

        • Tom Porterfield

          #5
          Re: Evaluate true in C# using switch

          Bruce Wood wrote:[color=blue]
          > Your way of writing switch statements looks more like a COBOL style[/color]

          Just an FYI that isn't really relevant. You can also write that style of
          switch statement in VB(6 and .NET).

          Select Case True
          Case Header.ParentSe ctionDisplayNam e = "":
          MyString = Header.SectionD isplayName
          Case Header.ParentSe ctionDisplayNam e <> "" And
          Header.Breadcru mb.ParentSectio nCategoryDispla yName = "":
          HBXPageName = Header.SectionD isplayName
          End Select
          --
          Tom Porterfield


          Comment

          Working...