Dim inside Select case executes in any case?

Discussion in 'ASP General' started by aa, Apr 23, 2007.

  1. aa

    aa Guest

    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?
    aa, Apr 23, 2007
    #1
    1. Advertising

  2. 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"
    Bob Barrows [MVP], Apr 23, 2007
    #2
    1. Advertising

  3. aa

    aa Guest

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

    "Bob Barrows [MVP]" <> wrote in message
    news:O6i3#...
    > 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"
    >
    >
    aa, Apr 23, 2007
    #3
  4. 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#...
    >> 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"
    Bob Barrows [MVP], Apr 23, 2007
    #4
  5. aa

    aa Guest

    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#...
    > 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#...
    > >> 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"
    >
    >
    aa, Apr 23, 2007
    #5
  6. aa

    aa Guest

    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#...
    > 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#...
    > >> 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"
    >
    >
    aa, Apr 23, 2007
    #6
  7. 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.
    Bob Barrows [MVP], Apr 23, 2007
    #7
  8. 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.
    Bob Barrows [MVP], Apr 23, 2007
    #8
  9. 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.
    Dave Anderson, Apr 23, 2007
    #9
  10. aa

    aa Guest

    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$...
    > 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.
    >
    >
    aa, Apr 23, 2007
    #10
  11. 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.
    Bob Barrows [MVP], Apr 23, 2007
    #11
  12. aa

    aa Guest

    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:#...
    > 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.
    >
    >
    aa, Apr 23, 2007
    #12
  13. 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.
    Dave Anderson, Apr 23, 2007
    #13
  14. aa

    aa Guest

    Dave, are you OK?
    "Dave Anderson" <> wrote in message
    news:OEI#...
    > 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.
    >
    >
    aa, Apr 24, 2007
    #14
  15. aa

    aa Guest

    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.
    aa, Apr 24, 2007
    #15
    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. Tee
    Replies:
    3
    Views:
    7,792
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. Denis Palas
    Replies:
    1
    Views:
    500
  3. Replies:
    2
    Views:
    344
    mlimber
    May 22, 2006
  4. Florian Kaufmann
    Replies:
    1
    Views:
    374
  5. Francisco Laguna

    "case when" executes a symbol as method

    Francisco Laguna, Jun 14, 2007, in forum: Ruby
    Replies:
    5
    Views:
    133
    Nobuyoshi Nakada
    Jun 17, 2007
Loading...

Share This Page