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

  • Thread starter Lakshmi Narayanan.R
  • Start date
L

Lakshmi Narayanan.R

Hi Experts,

Using keyword "To" in select case giving error.The following code is got
from www.microsrosoft.com itself. What is the wrong with this?.

<%
Dim Number1
Number1 = 7 ' Initialize variable.
Select Case Number1 ' Evaluate Number1.
Case 1 To 5 ' Number1 between 1 and 5, inclusive.
Response.Write( "Between 1 and 5" )
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8 ' Number1 between 6 and 8.
Response.Write( "Between 6 and 8")
Case 9 To 10 ' Number1 is 9 or 10.
Response.Write( "Greater than 8")
Case Else ' Other values.
Response.Write( "Not between 1 and 10")
End Select
%>

Thanx in advance
Laks.R
 
B

Bob Barrows [MVP]

Lakshmi said:
Hi Experts,

Using keyword "To" in select case giving error.The following code is
got from www.microsrosoft.com itself. What is the wrong with this?.

The code you you got was written for VB/VBA. vbscript is not VB. The To
keyword is not supported in vbscript.
<%
Dim Number1
Number1 = 7 ' Initialize variable.
Select Case Number1 ' Evaluate Number1.
Case 1 To 5 ' Number1 between 1 and 5, inclusive.
Response.Write( "Between 1 and 5" )

This should be:
Response.Write "Between 1 and 5"

Only use parentheses in VB/VBA/vbscript when calling a function whose return
value you are consuming. If you were calling a method with more than one
argument without consuming the return value, using parentheses would raise
an error.

By "consuming the return value" I mean:

1. Setting the return value to a variable:
Set a = createobject("some.class")
Parentheses are required

2. Evaluating the return value:
Function test(x,y)
if x>y then
test=true
else
test=false
end if
end Function
if test(10,5) then ...

Again, parentheses are required.

Calling test without consuming the return value requires that it be called
like a Sub (with no parentheses):

test 10, 5

unless using the Call statement:
Call test(10,5)

More here:
http://weblogs.asp.net/ericlippert/archive/2003/09/15/52996.aspx


HTH,
Bob Barrows
 
E

Evertjan.

Bob Barrows [MVP] wrote on 02 mrt 2005 in
microsoft.public.inetserver.asp.general:
2. Evaluating the return value:
Function test(x,y)
if x>y then
test=true
else
test=false
end if
end Function

Why not simply:

Function test(x,y)
test = x>y
end Function
 
B

Bob Barrows [MVP]

Evertjan. said:
Bob Barrows [MVP] wrote on 02 mrt 2005 in
microsoft.public.inetserver.asp.general:

Why not simply:

Function test(x,y)
test = x>y
end Function
2 reasons:
Clarity - While yours is more efficient, mine is clearer to the newbie. If I
was writing it myself, I would use your version.

The real reason: I originally wrote a different example, and when I changed
it to this, I was too lazy to rewrite it using the more efficient version.
:)

Bob Barrows
 
L

Lakshmi Narayanan.R

Hi MVP's

Did u run the code, i have pasted.

What u said is correct for Functions other than comes in collections.
So, writing Response.Write with paranthesis will work fine.
My question is that the error comes in the line "case 1 to 5".

So pls run the code run the code run the code and find the solution.

Regards
Laks.R
 
B

Bob Barrows [MVP]

Lakshmi said:
Hi MVP's

Did u run the code, i have pasted.

What u said is correct for Functions other than comes in collections.
So, writing Response.Write with paranthesis will work fine.

Of course it works fine (sort of): there's only one argument. That does not
mean it is correct. You are forcing the compiler to do a little extra work.
Granted, you will not notice the extra work it's doing, but there is no
reason to make it do it.
My question is that the error comes in the line "case 1 to 5".

I answered that question. I understand there may be a language difference
between us, but I did answer this.
So pls run the code run the code run the code and find the solution.

There is no need to run it.
There is no solution for this in vbscript. It works in Visual Basic. It does
NOT work in vbscript.
There IS a workaround:

Select Case true
Case (Number1>=1 and Number1 <=5)
...
End Select

Bob Barrows
 
L

Lakshmi Narayanan.R

Hi MVP's

Thank u for ur reply.
Removing the paranthesis also giving the same error for the word "TO" in the
case 1 to 5

The error is here

Microsoft VBScript compilation error '800a0400'

Expected statement

/asp/casestat.asp, line 5

Case 1 To 5 ' Number1 between 1 and 5, inclusive.
-------^

So pls take a little effort and reply me.
Thanx for ur suggession

Laks.R
 
B

Bob Barrows [MVP]

Lakshmi said:
Hi MVP's

Thank u for ur reply.
Removing the paranthesis also giving the same error for the word "TO"

The parentheses issue was not related to the "TO" issue. It was a side
issue. I answered your question about "To" with my very first sentences:
The code you you got was written for VB/VBA. vbscript is not VB. The To
keyword is not supported in vbscript.
Then in my second message:
There is no solution for this in vbscript. It works in Visual Basic. It does
NOT work in vbscript.
There IS a workaround:
<snip - go back and read it yourself - it's time YOU made the effort to
understand what I'm saying to you>

For the last time:
The "TO" keyword is NOT LEGAL in the vbscript Case statement.
You can download the vbscript documentation from here:
http://tinyurl.com/7rk6

So pls take a little effort and reply me.

I provided a workaround for you in my previous reply. PLEASE READ THE ENTIRE
MESSAGE! This is the second time you have accused me of not making any
effort for you. It will be the last.

Bob Barrows
 
R

Roland Hall

in message
: Lakshmi Narayanan.R wrote:
: > Hi MVP's
: >
: > Thank u for ur reply.
: > Removing the paranthesis also giving the same error for the word "TO"
:
: The parentheses issue was not related to the "TO" issue. It was a side
: issue. I answered your question about "To" with my very first sentences:
:
: >>>
: The code you you got was written for VB/VBA. vbscript is not VB. The To
: keyword is not supported in vbscript.
: >>>
:
: Then in my second message:
:
: >>>
: There is no solution for this in vbscript. It works in Visual Basic. It
does
: NOT work in vbscript.
: There IS a workaround:
: <snip - go back and read it yourself - it's time YOU made the effort to
: understand what I'm saying to you>
: >>>
:
:
: For the last time:
: The "TO" keyword is NOT LEGAL in the vbscript Case statement.
: You can download the vbscript documentation from here:
: http://tinyurl.com/7rk6
:
: <snip>
: >
: > So pls take a little effort and reply me.
:
: I provided a workaround for you in my previous reply. PLEASE READ THE
ENTIRE
: MESSAGE! This is the second time you have accused me of not making any
: effort for you. It will be the last.

You know Bob, I'm so confused now. Could you go over it just one more time
and what about that keyword IS, can I use that instead? (O:=

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
L

Lakshmi Narayanan.R

I beg very very sorry Mr.Bob,

As u said, i didnt read carefully after the first para.Seeing the replyies
in small window, i did the mistake.

Pls dont mistaken me.

Thank u for ur solutions.

Regards
Laks.R
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top