Switch Statement Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott C. Reynolds

    #1

    Switch Statement Help

    In VB6 you could do a SELECT CASE that would evaluate each case for
    truth and execute those statements, such as:

    SELECT CASE True
    case x > y:
    dosomestuff()
    case x = 5:
    dosomestuff()
    case y > x:
    dosomestuff()
    END SELECT

    such that if x were 5 and y were 3, it would execute both statements
    that evaluated to true.

    I have found no way to do the same or similar using Switch. Is there any?

  • Selvin

    #2
    Re: Switch Statement Help

    <ciach>

    RTFM!!!!!!!

    from
    MSDN(ms-help://MS.VSCC/MS.MSDNVS/csref/html/vclrfTheSwitchS tatement.htm)

    switch (expression)
    {
    case constant-expression:
    statement
    jump-statement
    [default:
    statement
    jump-statement]
    }


    pozdrawiam

    --
    Selvin - Przemek Sulikowski


    Comment

    • Scott C. Reynolds

      #3
      Re: Switch Statement Help

      Selvin wrote:
      [color=blue]
      > <ciach>
      >
      > RTFM!!!!!!!
      >
      > from
      > MSDN(ms-help://MS.VSCC/MS.MSDNVS/csref/html/vclrfTheSwitchS tatement.htm)
      >
      > switch (expression)
      > {
      > case constant-expression:
      > statement
      > jump-statement
      > [default:
      > statement
      > jump-statement]
      > }
      >
      >
      > pozdrawiam
      >[/color]
      yes, thank you for your "RTFM" and your copy-paste from msdn...i can
      read the documentation too...and I know how to use a switch
      statement...wha t I was looking for, if you had bothered to read my
      message at all, was a way to replicate ONE PARTICULAR ASPECT of the VB
      select Case statement that I could not seem to replicate in a C# Switch
      statement, i.e., evaluating an expression, rather than a constant, for
      truth.

      Comment

      • Ignacio Machin

        #4
        Re: Switch Statement Help

        Hi Scott,

        I'm afraid that you cannot do that.

        If you look at the definition of the switch statement in C#:

        switch (expression)
        {
        case constant-expression:
        statement
        jump-statement
        [default:
        statement
        jump-statement]
        }


        you see that the case expression have to be an constant-expression, you
        cannot use an expression there. also the expression of the switch clause
        must be either integer or string.
        Also the jump-statement( break,continue) is a MUST therefore is not
        possible to have explicit fall through as in C++.

        Hope this help,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        "Scott C. Reynolds" <sreynolds17@ho tmail.com> wrote in message
        news:FH_7b.1346 67$0v4.9858577@ bgtnsc04-news.ops.worldn et.att.net...[color=blue]
        > In VB6 you could do a SELECT CASE that would evaluate each case for
        > truth and execute those statements, such as:
        >
        > SELECT CASE True
        > case x > y:
        > dosomestuff()
        > case x = 5:
        > dosomestuff()
        > case y > x:
        > dosomestuff()
        > END SELECT
        >
        > such that if x were 5 and y were 3, it would execute both statements
        > that evaluated to true.
        >
        > I have found no way to do the same or similar using Switch. Is there any?
        >[/color]


        Comment

        • Selvin

          #5
          Re: Switch Statement Help

          > yes, thank you for your "RTFM" and your copy-paste from msdn...i can[color=blue]
          > read the documentation too...and I know how to use a switch
          > statement...wha t I was looking for, if you had bothered to read my
          > message at all, was a way to replicate ONE PARTICULAR ASPECT of the VB
          > select Case statement that I could not seem to replicate in a C# Switch
          > statement, i.e., evaluating an expression, rather than a constant, for
          > truth.
          >[/color]
          it's 'cause C# is similar to C++ more then VB6 :)

          [color=blue]
          >SELECT CASE True
          >case x > y:
          >dosomestuff( )
          >case x = 5:
          >dosomestuff( )
          >case y > x:
          >dosomestuff( )
          >END SELECT[/color]

          use if statment
          there is no other way
          f.e:

          if(x>y)
          {
          robcstam();
          }
          else
          {
          if(x=5)
          {
          robcostam();
          }
          else
          {
          if(y>x)
          {
          robcosinnego()
          }
          }
          }

          pozdrawiam

          --
          Selvin - Przemek Sulikowski


          Comment

          • Scott C. Reynolds

            #6
            Re: Switch Statement Help

            Ignacio Machin wrote:
            [color=blue]
            > Hi Scott,
            >
            > I'm afraid that you cannot do that.
            >
            > If you look at the definition of the switch statement in C#:
            >
            > switch (expression)
            > {
            > case constant-expression:
            > statement
            > jump-statement
            > [default:
            > statement
            > jump-statement]
            > }
            >
            >
            > you see that the case expression have to be an constant-expression, you
            > cannot use an expression there. also the expression of the switch clause
            > must be either integer or string.
            > Also the jump-statement( break,continue) is a MUST therefore is not
            > possible to have explicit fall through as in C++.
            >
            > Hope this help,
            >[/color]
            well...it helps in that at least I know for sure that it can't be done.
            I thought maybe someone knew of a way to make it work. Oh well...

            So would you say the proper way to do it then would be:
            if(x>y)
            {}
            if(y<x)
            {}
            if(x=5)
            {}

            that seems like a lot of annoying ifs...at least they're not nested though.

            thanks for the help.

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Switch Statement Help

              Scott,[color=blue]
              > I have found no way to do the same or similar using Switch. Is there any?[/color]
              Unfortunately (or is it fortunately) there no equivalent switch in C#.

              Try searching this newsgroup at groups.google.c om there have been a couple
              of discussions in the last month or two on why it should or should not be
              supported...

              I would suggest an if/else if/else if/else construct.

              Hope this helps
              Jay

              "Scott C. Reynolds" <sreynolds17@ho tmail.com> wrote in message
              news:FH_7b.1346 67$0v4.9858577@ bgtnsc04-news.ops.worldn et.att.net...[color=blue]
              > In VB6 you could do a SELECT CASE that would evaluate each case for
              > truth and execute those statements, such as:
              >
              > SELECT CASE True
              > case x > y:
              > dosomestuff()
              > case x = 5:
              > dosomestuff()
              > case y > x:
              > dosomestuff()
              > END SELECT
              >
              > such that if x were 5 and y were 3, it would execute both statements
              > that evaluated to true.
              >
              > I have found no way to do the same or similar using Switch. Is there any?
              >[/color]


              Comment

              • Scott C. Reynolds

                #8
                Re: Switch Statement Help

                Jay B. Harlow [MVP - Outlook] wrote:
                [color=blue]
                > Scott,
                >[color=green]
                >>I have found no way to do the same or similar using Switch. Is there any?[/color]
                >
                > Unfortunately (or is it fortunately) there no equivalent switch in C#.
                >[/color]
                Well, I don't know if it's unfortunate or fortunate...I'v e been of the
                belief that most of the things not in C# that we could do in VB were
                fortunate, because we get forced to do things "the right way". Given
                everything else I can do in C#...i'll live without this.

                thanks.
                [color=blue]
                > Try searching this newsgroup at groups.google.c om there have been a couple
                > of discussions in the last month or two on why it should or should not be
                > supported...
                >
                > I would suggest an if/else if/else if/else construct.
                >
                > Hope this helps
                > Jay
                >
                > "Scott C. Reynolds" <sreynolds17@ho tmail.com> wrote in message
                > news:FH_7b.1346 67$0v4.9858577@ bgtnsc04-news.ops.worldn et.att.net...
                >[color=green]
                >>In VB6 you could do a SELECT CASE that would evaluate each case for
                >>truth and execute those statements, such as:
                >>
                >>SELECT CASE True
                >>case x > y:
                >>dosomestuff ()
                >>case x = 5:
                >>dosomestuff ()
                >>case y > x:
                >>dosomestuff ()
                >>END SELECT
                >>
                >>such that if x were 5 and y were 3, it would execute both statements
                >>that evaluated to true.
                >>
                >>I have found no way to do the same or similar using Switch. Is there any?
                >>[/color]
                >
                >
                >[/color]

                Comment

                • Selvin

                  #9
                  Re: Switch Statement Help

                  > So would you say the proper way to do it then would be:[color=blue]
                  > if(x>y)
                  > { function0 }
                  > if(y<x)
                  > { function1 }
                  > if(x=5)
                  > { function2 }[/color]
                  no because
                  if x =5 and x>y
                  your aplication use function0 and function2
                  what is not you point
                  see my post bellow

                  --
                  Selvin - Przemek Sulikowski


                  Comment

                  • Günter Prossliner

                    #10
                    Re: Switch Statement Help

                    Ich think you're wrong:

                    Dim d&
                    d = 10

                    Select Case d
                    Case Is < 20
                    Debug.Print "<20"
                    Case Is > 5
                    Debug.Print ">5"
                    End Select


                    This VB6 Code prints "<20". If the first condition is true, the others are
                    not evaluated

                    mfg GP

                    "Scott C. Reynolds" <sreynolds17@ho tmail.com> wrote in message
                    news:FH_7b.1346 67$0v4.9858577@ bgtnsc04-news.ops.worldn et.att.net...[color=blue]
                    > In VB6 you could do a SELECT CASE that would evaluate each case for
                    > truth and execute those statements, such as:
                    >
                    > SELECT CASE True
                    > case x > y:
                    > dosomestuff()
                    > case x = 5:
                    > dosomestuff()
                    > case y > x:
                    > dosomestuff()
                    > END SELECT
                    >
                    > such that if x were 5 and y were 3, it would execute both statements
                    > that evaluated to true.
                    >
                    > I have found no way to do the same or similar using Switch. Is there any?
                    >[/color]


                    Comment

                    • Scott C. Reynolds

                      #11
                      Re: Switch Statement Help

                      Günter Prossliner wrote:[color=blue]
                      > Ich think you're wrong:
                      >
                      > Dim d&
                      > d = 10
                      >
                      > Select Case d
                      > Case Is < 20
                      > Debug.Print "<20"
                      > Case Is > 5
                      > Debug.Print ">5"
                      > End Select
                      >
                      >
                      > This VB6 Code prints "<20". If the first condition is true, the others are
                      > not evaluated
                      >
                      > mfg GP
                      >[/color]

                      Try:

                      Dim d
                      d = 100
                      Dim c
                      c = "hi"

                      Select Case True
                      Case d < 20:
                      Debug.Print "<20"
                      Case d = 10:
                      Debug.Print "==10"
                      Case c = "hi":
                      Debug.Print "hi"
                      End Select

                      Select Case True (as opposed to "Select Case variable" evaluates every
                      case until it comes to the first true one, and each case expression can
                      be basically anything that evaluates to true or false. in this case we
                      can evaluate different expressions for different variables or objects
                      with the same select case construct, whereas in c#, you have to specify
                      an expression you want to evaluate (like Switch(d) and then switch(c)
                      separately)

                      Perhaps my original example was poor. Sorry.
                      [color=blue]
                      > "Scott C. Reynolds" <sreynolds17@ho tmail.com> wrote in message
                      > news:FH_7b.1346 67$0v4.9858577@ bgtnsc04-news.ops.worldn et.att.net...
                      >[color=green]
                      >>In VB6 you could do a SELECT CASE that would evaluate each case for
                      >>truth and execute those statements, such as:
                      >>
                      >>SELECT CASE True
                      >>case x > y:
                      >>dosomestuff ()
                      >>case x = 5:
                      >>dosomestuff ()
                      >>case y > x:
                      >>dosomestuff ()
                      >>END SELECT
                      >>
                      >>such that if x were 5 and y were 3, it would execute both statements
                      >>that evaluated to true.
                      >>
                      >>I have found no way to do the same or similar using Switch. Is there any?
                      >>[/color]
                      >
                      >
                      >[/color]

                      Comment

                      • Günter Prossliner

                        #12
                        Re: Switch Statement Help

                        > Select Case True (as opposed to "Select Case variable" evaluates every[color=blue]
                        > case until it comes to the first true one, and each case expression can
                        > be basically anything that evaluates to true or false. ...[/color]

                        Ok, the behavior is still the same. After the first expression if evaluated
                        true, the rest will not take affect.

                        So the C# of

                        SELECT CASE True
                        case x > y:
                        Debug.Print "1"
                        case x = 5:
                        Debug.Print "2"
                        case y > x:
                        Debug.Print "3"
                        END SELECT


                        would be:

                        if(x>=y)
                        {Console.WriteL ine("1");}
                        else if(y=<x)
                        {Console.WriteL ine("2");}
                        else if(x=5)
                        {Console.WriteL ine("3");}

                        NOT

                        if(x>=y)
                        {Console.WriteL ine("1");}
                        if(y<=x)
                        {Console.WriteL ine("2");}
                        if(x=5)
                        {Console.WriteL ine("3");}

                        Assuming x==5, y ==5

                        Result 1:
                        1

                        Result 2:
                        1
                        2
                        3

                        (I've replaced < / > with <= / >= to demonstrate the difference!)

                        Why don't you like the ifs? IMO the code is better readable than the VB
                        code.

                        GP


                        Comment

                        Working...