Dim inside Select case executes in any case?

Dim inside Select case executes in any case?

am 23.04.2007 12:12:24 von 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?

Re: Dim inside Select case executes in any case?

am 23.04.2007 12:59:46 von reb01501

aa wrote:
> 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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Dim inside Select case executes in any case?

am 23.04.2007 13:17:21 von aa

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

"Bob Barrows [MVP]" wrote in message
news:O6i3#ZZhHHA.4980@TK2MSFTNGP02.phx.gbl...
> aa wrote:
> > 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
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
>

Re: Dim inside Select case executes in any case?

am 23.04.2007 13:20:37 von reb01501

aa wrote:
> 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 ...

>
> "Bob Barrows [MVP]" wrote in message
> news:O6i3#ZZhHHA.4980@TK2MSFTNGP02.phx.gbl...
>> aa wrote:
>>> 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
>>
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Dim inside Select case executes in any case?

am 23.04.2007 19:40:17 von 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

"Bob Barrows [MVP]" wrote in message
news:OO#wolZhHHA.4704@TK2MSFTNGP06.phx.gbl...
> aa wrote:
> > 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 ...
>
> >
> > "Bob Barrows [MVP]" wrote in message
> > news:O6i3#ZZhHHA.4980@TK2MSFTNGP02.phx.gbl...
> >> aa wrote:
> >>> 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
> >>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
>

Re: Dim inside Select case executes in any case?

am 23.04.2007 19:51:38 von aa

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

"Bob Barrows [MVP]" wrote in message
news:OO#wolZhHHA.4704@TK2MSFTNGP06.phx.gbl...
> aa wrote:
> > 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 ...
>
> >
> > "Bob Barrows [MVP]" wrote in message
> > news:O6i3#ZZhHHA.4980@TK2MSFTNGP02.phx.gbl...
> >> aa wrote:
> >>> 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
> >>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
>

Re: Dim inside Select case executes in any case?

am 23.04.2007 20:03:22 von reb01501

aa wrote:
> 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.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: Dim inside Select case executes in any case?

am 23.04.2007 20:09:49 von reb01501

aa wrote:
> 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.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: Dim inside Select case executes in any case?

am 23.04.2007 20:42:01 von Dave Anderson

aa wrote:
> 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?

http://blogs.msdn.com/ericlippert/archive/2004/06/18/159378. aspx



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

Re: Dim inside Select case executes in any case?

am 23.04.2007 21:47:58 von 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.

"Bob Barrows [MVP]" wrote in message
news:OYqm$EdhHHA.2332@TK2MSFTNGP04.phx.gbl...
> aa wrote:
> > 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.
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>

Re: Dim inside Select case executes in any case?

am 23.04.2007 21:56:58 von reb01501

aa wrote:
> 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.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Re: Dim inside Select case executes in any case?

am 23.04.2007 22:01:03 von 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)


"Dave Anderson" wrote in message
news:#OUmVcdhHHA.1388@TK2MSFTNGP05.phx.gbl...
> aa wrote:
> > 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?
>
> http://blogs.msdn.com/ericlippert/archive/2004/06/18/159378. aspx
>
>
>
> --
> 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.
>
>

Re: Dim inside Select case executes in any case?

am 23.04.2007 22:19:09 von Dave Anderson

top.
to
bottom
from
read
not
do
conversations
sentences,
Like
top-post.
not
do
Please

aa wrote:
>> http://blogs.msdn.com/ericlippert/archive/2004/06/18/159378. aspx
>>
> 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.


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

Re: Dim inside Select case executes in any case?

am 24.04.2007 08:21:58 von aa

Dave, are you OK?
"Dave Anderson" wrote in message
news:OEI#nSehHHA.4064@TK2MSFTNGP03.phx.gbl...
> top.
> to
> bottom
> from
> read
> not
> do
> conversations
> sentences,
> Like
> top-post.
> not
> do
> Please
>
> aa wrote:
> >> http://blogs.msdn.com/ericlippert/archive/2004/06/18/159378. aspx
> >>
> > 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.
>
>
> --
> 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.
>
>

Re: Dim inside Select case executes in any case?

am 24.04.2007 08:24:06 von aa

This is what I have. Thanks

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