Dim inside Select case executes in any case?

A

aa

Code like this
=======================
Select case q
Case "a"
Dim arr(5)
Case "b"
Dim arr(2)
end select
=====================
returns an error saying variable arr redefined.
Should it be like that or an I using Select incorrectly? Perhaps something
similar to break needs to be used?
 
B

Bob Barrows [MVP]

aa said:
Code like this
=======================
Select case q
Case "a"
Dim arr(5)
Case "b"
Dim arr(2)
end select
=====================
returns an error saying variable arr redefined.
Should it be like that or an I using Select incorrectly? Perhaps
something similar to break needs to be used?

You can only declare a static array once. You need to use a dynamic array:

dim arr()
Select case q
Case "a"
ReDim arr(5)
Case "b"
ReDim arr(2)
end select

You can get the vbscript documentation here:
http://tinyurl.com/7rk6
 
B

Bob Barrows [MVP]

aa said:
Thanks,
It looks like it should be ReDim at the first decalration

What makes you say that? It shouldn't. Start by declaring it with with Dim,
then use ReDim to redefine it. This really is well-covered in the
documentation ...
 
A

aa

The reason I said that that initially I did exactly as you said - declared
it Dim
and then ReDim. As a result I got an error saing that the arrey cannot be
ReDimed for it decleared as a fixed length array.
When I changed to ReDim it worked
 
A

aa

The main question however, was if ASP/VBS are designed so that Dim in Select
case is executed for each case?
 
B

Bob Barrows [MVP]

aa said:
The main question however, was if ASP/VBS are designed so that Dim in
Select case is executed for each case?

The answer to that is "no", as you discovered. The vbscript compiler
"hoists" variable declarations to the beginning of the procedure. There
is no conditional declaration.
 
B

Bob Barrows [MVP]

aa said:
The reason I said that that initially I did exactly as you said -
declared it Dim
and then ReDim. As a result I got an error saing that the arrey
cannot be ReDimed for it decleared as a fixed length array.

Then you did not do exactly as I said. My example used a dynamic array,
not a fixed-length array.
When I changed to ReDim it worked

If you add "option explicit" to your script, you will realize what you
did: you redimmed a variable that had not been declared, causing the
compiler to declare one for you.

This is a fixed-length (static) array:
dim arr(3)

Static arrays cannot be redimmed.
Only dynamic arrays can be redimmed:

dim arr()
....
redim arr(5)

I know I'm sounding like a broken record, but the documentation does
cover this topic.
 
A

aa

I discovered that "yes".
If it were "no" then wy code in my original message
=======================
Select case q
Case "a"
Dim arr(5)
Case "b"
Dim arr(2)
end select
=====================
returns an error saying variable arr redefined.
 
B

Bob Barrows [MVP]

aa said:
I discovered that "yes".

OK, I misread your question and answered "no" instead of "yes", but
supplied the correct explanation for the behavior.
If it were "no" then wy code in my original message
=======================
Select case q
Case "a"
Dim arr(5)
Case "b"
Dim arr(2)
end select
=====================
returns an error saying variable arr redefined.

Because both dim statements get "hoisted" to the beginning of the
procedure during compilation. Both statements. So at runtime, what gets
executed is:

Dim arr(5)
Dim arr(2) '---raises error
Select Case q
etc.

Again, there is no conditional declaration allowed in vbscript.
 
A

aa

So it looks like Dim is executed before other code on a page as if this
other code is not there. Therefore my Select case is stripped down to
Dim arr(5)
Dim arr(2)
 
D

Dave Anderson

top.
to
bottom
from
read
not
do
conversations
sentences,
Like
top-post.
not
do
Please
So it looks like Dim is executed before other code on a page as if
this other code is not there. Therefore my Select case is stripped
down to Dim arr(5)
Dim arr(2)

Not "executed", exactly, but you have the right idea.
 
A

aa

Dave, are you OK?
Dave Anderson said:
top.
to
bottom
from
read
not
do
conversations
sentences,
Like
top-post.
not
do
Please


Not "executed", exactly, but you have the right idea.


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

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top