Select Case

C

CJM

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
 
A

Aaron Bertrand [SQL Server MVP]

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
 
C

CJM

Aaron Bertrand said:
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.

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
 
E

Evertjan.

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

Aaron Bertrand [SQL Server MVP]

'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. ;-)
 
C

CJM

Aaron Bertrand said:
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....
 
D

Dave Anderson

CJM said:
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?
 
A

Aaron Bertrand [SQL Server MVP]

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
 
B

Bob Lehmann

Which I'd already reviewed of course... It's a bit sparse, and only
So, the documentation indicates the obvious, but it's not clear???

Bob Lehmann
 
A

Anthony Jones

CJM said:
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.


It's the VB syntax. When '< 0' didn't work, I wondered if the VB syntax was

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.


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 ;)
 
C

CJM

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
 
D

Dave Anderson

CJM said:
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.
 
C

CJM

Dave Anderson said:
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.
 
M

Mike Brind

CJM said:
Dave Anderson said:
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.
 
A

Anthony Jones

CJM said:
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.
 
C

CJM

Mike Brind said:
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
 
C

CJM

Dave Anderson said:
CJM said:
...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
 
M

Mike Brind

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

CJM

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top