Select Case

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

    Select Case

    I'm getting a syntax error with a Select Case statement:

    Select Case CSng(rs.fields( "Field1"))
    Case 0
    Response.Write "Test1"
    Case Is < 0 <<< Syntax Error
    Response.Write "Test2"
    Case Is > 0 <<< Syntax Error
    Response.Write "Test3"
    End Select

    As far as I can tell the syntax *is* correct. What am I missing?

    Thanks

    Chris



  • Aaron Bertrand [SQL Server MVP]

    #2
    Re: Select Case

    > I'm getting a syntax error with a Select Case statement:[color=blue]
    >
    > Select Case CSng(rs.fields( "Field1"))
    > Case 0
    > Response.Write "Test1"
    > Case Is < 0 <<< Syntax Error
    > Response.Write "Test2"
    > Case Is > 0 <<< Syntax Error
    > Response.Write "Test3"
    > End Select
    >
    > As far as I can tell the syntax *is* correct. What am I missing?[/color]

    You mean as far as you can tell, aside from the error message telling you
    that the syntax is not correct???

    I don't know why you're throwing "Is" in there, where did you find syntax
    that looked like that?

    Here is the documentation for select case:



    Personally, I suggest using IF...ELSE...END IF for range vs. exact value.

    f1 = CSng(rs.fields( "Field1"))
    if f1 = 0 then
    response.write "Test1"
    elseif f1 < 0 then
    response.write "Test2"
    else
    response.write "Test3"
    end if

    A


    Comment

    • CJM

      #3
      Re: Select Case


      "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartr eb.noraa> wrote in message
      news:e1zyqLFnGH A.4348@TK2MSFTN GP02.phx.gbl...[color=blue][color=green]
      >> I'm getting a syntax error with a Select Case statement:
      >>
      >> Select Case CSng(rs.fields( "Field1"))
      >> Case 0
      >> Response.Write "Test1"
      >> Case Is < 0 <<< Syntax Error
      >> Response.Write "Test2"
      >> Case Is > 0 <<< Syntax Error
      >> Response.Write "Test3"
      >> End Select
      >>
      >> As far as I can tell the syntax *is* correct. What am I missing?[/color]
      >
      > You mean as far as you can tell, aside from the error message telling you
      > that the syntax is not correct???
      >[/color]

      Yes that's exactly what I mean. Which is why I said it.

      'As far as I can tell' means, I haven't found anything to say why it may not
      be syntactically correct.
      [color=blue]
      > I don't know why you're throwing "Is" in there, where did you find syntax
      > that looked like that?
      >[/color]

      It's the VB syntax. When '< 0' didn't work, I wondered if the VB syntax was
      appropriate.
      [color=blue]
      > Here is the documentation for select case:
      >
      > http://msdn.microsoft.com/library/en...71eefb3b01.asp
      >[/color]

      Which I'd already reviewed of course... It's a bit sparse, and only
      indicates the obvious - that '< 0' or 'Is <0' does not qualify as a valid
      expression.
      [color=blue]
      > Personally, I suggest using IF...ELSE...END IF for range vs. exact value.
      >
      > f1 = CSng(rs.fields( "Field1"))
      > if f1 = 0 then
      > response.write "Test1"
      > elseif f1 < 0 then
      > response.write "Test2"
      > else
      > response.write "Test3"
      > end if
      >[/color]

      Of course, this route was always open to me, but I would have rather used
      Select Case for clarity.

      Elsewhere I have found a comment that '=' is the only comparison operator
      allowed in VBScript Select Case statement, and this operator is implicit.

      It's not entirely surprising, that VBScript has such shortcomings compared
      to VB, but I expected the 'official' sources to make this clear.

      CJM


      Comment

      • Evertjan.

        #4
        Re: Select Case

        CJM wrote on 30 jun 2006 in microsoft.publi c.inetserver.as p.general:
        [color=blue]
        > Elsewhere I have found a comment that '=' is the only comparison
        > operator allowed in VBScript Select Case statement, and this operator
        > is implicit.
        >[/color]

        Try [if you are sure the field is always a number]:

        what = CSng(rs.fields( "Field1"))
        Select Case true
        Case what = 0
        Response.Write "Test1"
        Case what < 0
        Response.Write "Test2"
        Case what > 0
        Response.Write "Test3"
        End Select

        I however prefer Aaron's if..elseif..the n solution.

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        • Aaron Bertrand [SQL Server MVP]

          #5
          Re: Select Case

          > 'As far as I can tell' means, I haven't found anything to say why it may[color=blue]
          > not be syntactically correct.[/color]

          To me, 'as far as I can tell' implies that you have found something that
          says it should be syntactically correct. But that was just my
          interpretation.
          [color=blue]
          > It's not entirely surprising, that VBScript has such shortcomings compared
          > to VB, but I expected the 'official' sources to make this clear.[/color]

          Microsoft isn't too great on documenting the limitations in their products.
          Especially ones they aren't actively developing. ;-)


          Comment

          • CJM

            #6
            Re: Select Case


            "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartr eb.noraa> wrote in message
            news:uSrdtrFnGH A.4448@TK2MSFTN GP04.phx.gbl...[color=blue]
            >[color=green]
            >> It's not entirely surprising, that VBScript has such shortcomings
            >> compared to VB, but I expected the 'official' sources to make this clear.[/color]
            >
            > Microsoft isn't too great on documenting the limitations in their
            > products. Especially ones they aren't actively developing. ;-)[/color]

            I realise that this isnt a priority anymore, but I would have expected
            better documentation precisely because ASP/VBScript has been such a key
            technology over the years. And I'm surprised that more independent sources
            didn't list this caveat....


            Comment

            • Dave Anderson

              #7
              Re: Select Case

              CJM wrote:[color=blue]
              > I realise that this isnt a priority anymore, but I would have
              > expected better documentation precisely because ASP/VBScript
              > has been such a key technology over the years. And I'm surprised
              > that more independent sources didn't list this caveat....[/color]

              What would you have them document, that incorrect syntax may not produce the
              desired results?



              --
              Dave Anderson

              Unsolicited commercial email will be read at a cost of $500 per message. Use
              of this email address implies consent to these terms.


              Comment

              • Aaron Bertrand [SQL Server MVP]

                #8
                Re: Select Case

                > What would you have them document, that incorrect syntax may not produce[color=blue]
                > the desired results?[/color]

                I was wondering what to expect.

                Maybe also, "if you are used to switch in JavaScript, here is how those
                differ..."

                A


                Comment

                • Bob Lehmann

                  #9
                  Re: Select Case

                  >> Which I'd already reviewed of course... It's a bit sparse, and only[color=blue][color=green]
                  >> indicates the obvious - that '< 0' or 'Is <0' does not qualify as a valid
                  >> expression.[/color][/color]
                  [color=blue][color=green]
                  >> but I expected the 'official' sources to make this clear.[/color][/color]

                  So, the documentation indicates the obvious, but it's not clear???

                  Bob Lehmann


                  "CJM" <cjmnews04@news group.nospam> wrote in message
                  news:Od$ODcFnGH A.4212@TK2MSFTN GP04.phx.gbl...[color=blue]
                  >
                  > "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartr eb.noraa> wrote in[/color]
                  message[color=blue]
                  > news:e1zyqLFnGH A.4348@TK2MSFTN GP02.phx.gbl...[color=green][color=darkred]
                  > >> I'm getting a syntax error with a Select Case statement:
                  > >>
                  > >> Select Case CSng(rs.fields( "Field1"))
                  > >> Case 0
                  > >> Response.Write "Test1"
                  > >> Case Is < 0 <<< Syntax Error
                  > >> Response.Write "Test2"
                  > >> Case Is > 0 <<< Syntax Error
                  > >> Response.Write "Test3"
                  > >> End Select
                  > >>
                  > >> As far as I can tell the syntax *is* correct. What am I missing?[/color]
                  > >
                  > > You mean as far as you can tell, aside from the error message telling[/color][/color]
                  you[color=blue][color=green]
                  > > that the syntax is not correct???
                  > >[/color]
                  >
                  > Yes that's exactly what I mean. Which is why I said it.
                  >
                  > 'As far as I can tell' means, I haven't found anything to say why it may[/color]
                  not[color=blue]
                  > be syntactically correct.
                  >[color=green]
                  > > I don't know why you're throwing "Is" in there, where did you find[/color][/color]
                  syntax[color=blue][color=green]
                  > > that looked like that?
                  > >[/color]
                  >
                  > It's the VB syntax. When '< 0' didn't work, I wondered if the VB syntax[/color]
                  was[color=blue]
                  > appropriate.
                  >[color=green]
                  > > Here is the documentation for select case:
                  > >
                  > >[/color][/color]
                  http://msdn.microsoft.com/library/en...71eefb3b01.asp[color=blue][color=green]
                  > >[/color]
                  >
                  > Which I'd already reviewed of course... It's a bit sparse, and only
                  > indicates the obvious - that '< 0' or 'Is <0' does not qualify as a valid
                  > expression.
                  >[color=green]
                  > > Personally, I suggest using IF...ELSE...END IF for range vs. exact[/color][/color]
                  value.[color=blue][color=green]
                  > >
                  > > f1 = CSng(rs.fields( "Field1"))
                  > > if f1 = 0 then
                  > > response.write "Test1"
                  > > elseif f1 < 0 then
                  > > response.write "Test2"
                  > > else
                  > > response.write "Test3"
                  > > end if
                  > >[/color]
                  >
                  > Of course, this route was always open to me, but I would have rather used
                  > Select Case for clarity.
                  >
                  > Elsewhere I have found a comment that '=' is the only comparison operator
                  > allowed in VBScript Select Case statement, and this operator is implicit.
                  >
                  > It's not entirely surprising, that VBScript has such shortcomings compared
                  > to VB, but I expected the 'official' sources to make this clear.
                  >
                  > CJM
                  >
                  >[/color]


                  Comment

                  • Dave Anderson

                    #10
                    Re: Select Case

                    Aaron Bertrand [SQL Server MVP] wrote:[color=blue]
                    > I was wondering what to expect.
                    >
                    > Maybe also, "if you are used to switch in JavaScript, here
                    > is how those differ..."[/color]

                    Even if that were the case, the syntax he provided bears no resemblance to
                    the JScript switch syntax, so I still am left baffled by his expectations.



                    --
                    Dave Anderson

                    Unsolicited commercial email will be read at a cost of $500 per message. Use
                    of this email address implies consent to these terms.


                    Comment

                    • Anthony Jones

                      #11
                      Re: Select Case


                      "CJM" <cjmnews04@news group.nospamwro te in message
                      news:Od$ODcFnGH A.4212@TK2MSFTN GP04.phx.gbl...
                      >
                      "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartr eb.noraawrote in
                      message
                      news:e1zyqLFnGH A.4348@TK2MSFTN GP02.phx.gbl...
                      I'm getting a syntax error with a Select Case statement:
                      >
                      Select Case CSng(rs.fields( "Field1"))
                      Case 0
                      Response.Write "Test1"
                      Case Is < 0 <<< Syntax Error
                      Response.Write "Test2"
                      Case Is 0 <<< Syntax Error
                      Response.Write "Test3"
                      End Select
                      >
                      As far as I can tell the syntax *is* correct. What am I missing?
                      You mean as far as you can tell, aside from the error message telling
                      you
                      that the syntax is not correct???
                      >
                      Yes that's exactly what I mean. Which is why I said it.
                      >
                      'As far as I can tell' means, I haven't found anything to say why it may
                      not
                      be syntactically correct.
                      >
                      I don't know why you're throwing "Is" in there, where did you find
                      syntax
                      that looked like that?
                      >
                      It's the VB syntax. When '< 0' didn't work, I wondered if the VB syntax
                      was
                      appropriate.
                      >
                      Here is the documentation for select case:
                      http://msdn.microsoft.com/library/en...71eefb3b01.asp
                      >
                      Which I'd already reviewed of course... It's a bit sparse, and only
                      indicates the obvious - that '< 0' or 'Is <0' does not qualify as a valid
                      expression.
                      >
                      Personally, I suggest using IF...ELSE...END IF for range vs. exact
                      value.

                      f1 = CSng(rs.fields( "Field1"))
                      if f1 = 0 then
                      response.write "Test1"
                      elseif f1 < 0 then
                      response.write "Test2"
                      else
                      response.write "Test3"
                      end if
                      >
                      Of course, this route was always open to me, but I would have rather used
                      Select Case for clarity.
                      >
                      Elsewhere I have found a comment that '=' is the only comparison operator
                      allowed in VBScript Select Case statement, and this operator is implicit.
                      >
                      It's not entirely surprising, that VBScript has such shortcomings compared
                      to VB, but I expected the 'official' sources to make this clear.
                      >
                      CJM

                      I don't think the documentation is at fault here. There is no assumption in
                      the documentation that the reader is already familiar with VB thereby
                      needing guidance as to the differences. The standard definition of an
                      'expression' is assumed by the documentation. There is nothing there that
                      implies that the testexpression would form the first operand in any partial
                      expression in any Case expressionlist. The documentation is not being
                      'sparse' by not describing things you might think you can do but actually
                      can't, such documentation would be impossible to read.

                      Just my pennies worth ;)


                      Comment

                      • CJM

                        #12
                        Re: Select Case

                        The MSDN VB documentation states that 'Case <expressionli st-n>' is the
                        correct syntax. So does the MSDN VBScript documentation.

                        I made the incorrect assumption (or perhaps an expectation) that MSDN would
                        regard these as being the same, when they aren't.


                        Update: When I was complaining about the documentation being sparse, I've
                        just realised that most of the information is being hidden, since I was
                        viewing the page in Firefox. The basic syntax is shown but there are no
                        accompanying notes. In Opera 9, it doesnt shown anything. Check for
                        yourselves. [So I'm not going mad after all....]

                        Having now attempted to view the page in IE6, I can now see the comments
                        which does indicate that comma-separated expression are allowed...

                        However, we are back to the argument of the mean of the word 'expression'.
                        According to the MSDN VB documentation 'Is < 0' is a valid expression... so
                        why wouldn't it be valid for VBScript? Surely, the limits to the type of
                        acceptable expressions should be stated?





                        Comment

                        • Dave Anderson

                          #13
                          Re: Select Case

                          CJM wrote:
                          However, we are back to the argument of the mean of the
                          word 'expression'. According to the MSDN VB documentation
                          'Is < 0' is a valid expression... so why wouldn't it be
                          valid for VBScript?
                          For one thing, VB is not VBScript. And [IS] has a very specific purpose in
                          VBScript -- the comparison of *objects*:



                          Lastly, in VBScript, [IS] requires two operands. Your expression does not
                          satisfy this requirement.



                          --
                          Dave Anderson

                          Unsolicited commercial email will be read at a cost of $500 per message. Use
                          of this email address implies consent to these terms.


                          Comment

                          • CJM

                            #14
                            Re: Select Case


                            "Dave Anderson" <NYRUMTPELVWH@s pammotel.comwro te in message
                            news:OsPLRdqnGH A.4728@TK2MSFTN GP03.phx.gbl...
                            >
                            For one thing, VB is not VBScript. And [IS] has a very specific purpose in
                            VBScript -- the comparison of *objects*:
                            >
                            Of course VB is not VBScript. I have never indicated anything to the
                            contrary, but they *are* closely related, so when I couldnt find the
                            information I wanted for VBScript, I looked for inspiration from VB.


                            Comment

                            • Mike Brind

                              #15
                              Re: Select Case


                              CJM wrote:
                              "Dave Anderson" <NYRUMTPELVWH@s pammotel.comwro te in message
                              news:OsPLRdqnGH A.4728@TK2MSFTN GP03.phx.gbl...

                              For one thing, VB is not VBScript. And [IS] has a very specific purpose in
                              VBScript -- the comparison of *objects*:
                              >
                              Of course VB is not VBScript. I have never indicated anything to the
                              contrary, but they *are* closely related, so when I couldnt find the
                              information I wanted for VBScript, I looked for inspiration from VB.
                              Not always a bad idea, but given that it didn't work in this case, it
                              seems pretty pointless complaining that it didn't work, just as it's
                              pretty pointless complaining that any official documentation doesn't
                              explicity exclude all the infinite number of possiblities one could
                              come up with in terms of incorrect syntax.

                              --
                              Mike Brind

                              Comment

                              Working...