Select Case

Discussion in 'ASP General' started by CJM, Jun 30, 2006.

  1. CJM

    CJM Guest

    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
    CJM, Jun 30, 2006
    #1
    1. Advertising

  2. > 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???

    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:

    http://msdn.microsoft.com/library/en-us/script56/html/91c340af-8ceb-4f46-86fa-7871eefb3b01.asp

    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
    Aaron Bertrand [SQL Server MVP], Jun 30, 2006
    #2
    1. Advertising

  3. CJM

    CJM Guest

    "Aaron Bertrand [SQL Server MVP]" <> wrote in message
    news:...
    >> 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-us/script56/html/91c340af-8ceb-4f46-86fa-7871eefb3b01.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
    CJM, Jun 30, 2006
    #3
  4. CJM

    Evertjan. Guest

    CJM wrote on 30 jun 2006 in microsoft.public.inetserver.asp.general:

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


    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..then solution.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jun 30, 2006
    #4
  5. > 'As far as I can tell' means, I haven't found anything to say why it may
    > not be syntactically correct.


    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.

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


    Microsoft isn't too great on documenting the limitations in their products.
    Especially ones they aren't actively developing. ;-)
    Aaron Bertrand [SQL Server MVP], Jun 30, 2006
    #5
  6. CJM

    CJM Guest

    "Aaron Bertrand [SQL Server MVP]" <> wrote in message
    news:...
    >
    >> It's not entirely surprising, that VBScript has such shortcomings
    >> compared to VB, but I expected the 'official' sources to make this clear.

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


    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....
    CJM, Jun 30, 2006
    #6
  7. CJM wrote:
    > 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....


    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.
    Dave Anderson, Jun 30, 2006
    #7
  8. > What would you have them document, that incorrect syntax may not produce
    > the desired results?


    I was wondering what to expect.

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

    A
    Aaron Bertrand [SQL Server MVP], Jun 30, 2006
    #8
  9. CJM

    Bob Lehmann Guest

    >> 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.


    >> but I expected the 'official' sources to make this clear.


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

    Bob Lehmann


    "CJM" <> wrote in message
    news:Od$...
    >
    > "Aaron Bertrand [SQL Server MVP]" <> wrote in

    message
    > news:...
    > >> 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-us/script56/html/91c340af-8ceb-4f46-86fa-7871eefb3b01.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
    >
    >
    Bob Lehmann, Jul 1, 2006
    #9
  10. "CJM" <> wrote in message
    news:Od$...
    >
    > "Aaron Bertrand [SQL Server MVP]" <> wrote in

    message
    > news:...
    > >> 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-us/script56/html/91c340af-8ceb-4f46-86fa-7871eefb3b01.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 ;)
    Anthony Jones, Jul 3, 2006
    #10
  11. CJM

    CJM Guest

    The MSDN VB documentation states that 'Case <expressionlist-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?

    http://foldoc.org/foldoc.cgi?query=expression&action=Search
    CJM, Jul 3, 2006
    #11
  12. 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*:

    http://msdn.microsoft.com/library/en-us/script56/html/49dafc9c-d1de-4193-8685-1d3fad510189.asp

    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.
    Dave Anderson, Jul 3, 2006
    #12
  13. CJM

    CJM Guest

    "Dave Anderson" <> wrote in message
    news:...
    >
    > 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.
    CJM, Jul 3, 2006
    #13
  14. CJM

    Mike Brind Guest

    CJM wrote:
    > "Dave Anderson" <> wrote in message
    > news:...
    > >
    > > 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
    Mike Brind, Jul 3, 2006
    #14
  15. CJM wrote:
    > ...when I couldnt find the information I wanted for VBScript...


    I can certainly appreciate this statement. When looking for the [Select
    Case] documentation, I found that it was not listed here...
    http://msdn.microsoft.com/library/en-us/script56/html/294e3d21-795e-4f3d-b9df-c74cde3390cb.asp

    ....yet it is listed among the Statements in the left-pane menu. That's a bad
    oversight, IMO.



    --
    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.
    Dave Anderson, Jul 3, 2006
    #15
  16. "CJM" <> wrote in message
    news:...
    > The MSDN VB documentation states that 'Case <expressionlist-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?
    >


    The VB documentation very specifically highlights the special use of the
    keyword 'Is' in the context of a Case statement. The VBScript documentation
    does not. The VBScript documentation also omits reference to the 'To'
    keyword in the context of the Case statement and of course trying to use it
    also results in an error.

    > http://foldoc.org/foldoc.cgi?query=expression&action=Search
    >
    >
    >
    Anthony Jones, Jul 3, 2006
    #16
  17. CJM

    CJM Guest

    "Mike Brind" <> wrote in message
    news:...
    >
    > 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.
    >


    First of all, I'm not complaining it didn't work; I explicitly said that I
    was hoping for inspiration from the VB documentation but that I accepted its
    limitations.

    Secondly, I expect reserved terms like 'expressionlist' to be consistent
    across MSDN especially since the term 'expression' is not an MS-only term. I
    don't expect MSDN to detail the infiinite number of terms of incorrect
    syntax, but I would hope it would give a better indication of what is and
    isn't allowed. The fact is that I wanted to use a valid expression in the
    statement (an expression that MS regard as valid for VB) yet it isn't valid
    for VBScript, and on top of that, the most important MSDN help was hidden
    because of poor web design, so it was difficult to even infer the right
    information. More importantly, I geniunely expected VBScript to be able to
    handle this kind of expression, and I'm surprised it doesnt. And I'm
    surprised I didnt find any 3rd-party websites that remarked on this
    situation - I can't be the first person to hope for more out of the Select
    Case statement.

    What I would complain about (if it were any use) were the ratio of helpful
    responses to those that weren't helpful (like yours). I didn't start this
    thread to complain about VBScript - I wanted to find out an answer to
    satisfy my curiousity. I eventually got that answer, but not before a number
    of people tried to sour the thread with their unhelpful answers. If people
    put the same amount of effort into helping posters rather than starting
    conflict, this NG would be that much better.

    The unintentional insight I gained was that the majority of users seem to be
    locked into IE as their preferred UA (which surprised me).

    CJM
    CJM, Jul 4, 2006
    #17
  18. CJM

    CJM Guest

    "Dave Anderson" <> wrote in message
    news:...
    > CJM wrote:
    >> ...when I couldnt find the information I wanted for VBScript...

    >
    > I can certainly appreciate this statement. When looking for the [Select
    > Case] documentation, I found that it was not listed here...
    > http://msdn.microsoft.com/library/en-us/script56/html/294e3d21-795e-4f3d-b9df-c74cde3390cb.asp
    >
    > ...yet it is listed among the Statements in the left-pane menu. That's a
    > bad oversight, IMO.
    >
    >


    Dave,

    Are you sure? It's listed for me (in both IE & FF). Right column, third from
    bottom.

    CJM
    CJM, Jul 4, 2006
    #18
  19. CJM

    Mike Brind Guest

    CJM wrote:
    > "Mike Brind" <> wrote in message
    > news:...
    > >


    <snip>

    >
    > What I would complain about (if it were any use) were the ratio of helpful
    > responses to those that weren't helpful (like yours). I didn't start this
    > thread to complain about VBScript - I wanted to find out an answer to
    > satisfy my curiousity. I eventually got that answer, but not before a number
    > of people tried to sour the thread with their unhelpful answers. If people
    > put the same amount of effort into helping posters rather than starting
    > conflict, this NG would be that much better.


    I don't accept that as a true reflection of this group. Sometimes
    people need the blindingly obvious to be explained, and some posters
    will do so in a seemingly abrupt, possibly even a sarcastic way -
    although it is impossible to read motivation into a few short words
    most of the time.

    IMO this NG suffers much less from people deliberately trying to flame
    than any other I know of. It hardly ever happens, and I don't see that
    it necessarily did in this thread. True, my contribution wasn't
    helpful, and was probably better left unposted. If it irritated you, I
    apologise.

    >
    > The unintentional insight I gained was that the majority of users seem to be
    > locked into IE as their preferred UA (which surprised me).
    >


    I only use it to look at the online MSDN documentation. Their
    silly-arse dhtml doesn't work in Firefox :)

    Nor, incidentally, does this page: http://www.aspfaq.com/categories.asp
    .......

    --
    Mike Brind
    Mike Brind, Jul 4, 2006
    #19
  20. CJM

    CJM Guest

    "Mike Brind" <> wrote in message
    news:...

    >
    > I only use it to look at the online MSDN documentation. Their
    > silly-arse dhtml doesn't work in Firefox :)
    >


    You should try it with Opera. Seriously...

    > Nor, incidentally, does this page: http://www.aspfaq.com/categories.asp
    > ......
    >


    Hmmm... the internal category links don't work it appears...

    I notice the Aaron is going through a major overhaul, so maybe we should
    wait until it settles.

    [Aaron: have you gone live yet? The start date was 18/06/06, but it doesn't
    look like you've switched over yet]

    CJM
    CJM, Jul 4, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,247
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,792
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Lakshmi Narayanan.R

    "Case 1 To 5" in Select case, giving Error!!. Why?

    Lakshmi Narayanan.R, Mar 2, 2005, in forum: ASP General
    Replies:
    10
    Views:
    345
    Bob Barrows [MVP]
    Mar 4, 2005
  4. aa
    Replies:
    14
    Views:
    320
  5. palmiere
    Replies:
    1
    Views:
    395
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page