redim preserve myArray(i)

Discussion in 'ASP General' started by Guest, Jan 17, 2004.

  1. Guest

    Guest Guest

    Is it correct to think that after reducing the populated array's size from
    say, 10 to 5 with
    redim preserve myArray(i)
    an attempt to access an element above the fifth does not cause a
    compillation error "array out of script", but returns whatever heppened to
    be written in that memory address (in particular it might return the correct
    values of those elements before re-dimentioning)?
    This seems to be the case in my code, yet I wanted to make sure that this is
    not the result of some other side effect
     
    Guest, Jan 17, 2004
    #1
    1. Advertising

  2. Guest

    Bob Barrows Guest

    aa wrote:
    > Is it correct to think that after reducing the populated array's size
    > from say, 10 to 5 with
    > redim preserve myArray(i)
    > an attempt to access an element above the fifth does not cause a
    > compillation error "array out of script", but returns whatever
    > heppened to be written in that memory address (in particular it might
    > return the correct values of those elements before re-dimentioning)?
    > This seems to be the case in my code, yet I wanted to make sure that
    > this is not the result of some other side effect


    Nope. As expected, this code raises the "Subscript out of range: '5'" error
    on my machine:

    <%
    dim ar(),i
    redim ar(10)
    for i=0 to 10
    ar(i)=1
    next
    redim preserve ar(4)
    Response.Write ar(5)
    Response.End
    %>

    If you are using "on error resume next", and you attempt to redim an array
    that cannot be redim'ed (the only redimmable arrays are those declared with
    the syntax shown in my dim statement above), you may not receive the error
    message, and the array will be unaffected by the redim statement. That is
    what happens when I change my code to this:

    <%
    on error resume next
    dim ar(10), i
    for i=0 to 10
    ar(i)=i
    next
    redim preserve ar(4)
    for i = 0 to 10
    Response.Write ar(i) & "<BR>"
    next
    Response.End
    %>

    Bob Barrows
    --
    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, Jan 17, 2004
    #2
    1. Advertising

  3. Guest

    Guest Guest

    Thans, Bob, that's exactly what I wanted to know.
    My code redims an array up and down and thought it does not produce an
    error, sometimes the final result is incorrect.
    So I have an error elsewhere



    "Bob Barrows" <> wrote in message
    news:...
    > aa wrote:
    > > Is it correct to think that after reducing the populated array's size
    > > from say, 10 to 5 with
    > > redim preserve myArray(i)
    > > an attempt to access an element above the fifth does not cause a
    > > compillation error "array out of script", but returns whatever
    > > heppened to be written in that memory address (in particular it might
    > > return the correct values of those elements before re-dimentioning)?
    > > This seems to be the case in my code, yet I wanted to make sure that
    > > this is not the result of some other side effect

    >
    > Nope. As expected, this code raises the "Subscript out of range: '5'"

    error
    > on my machine:
    >
    > <%
    > dim ar(),i
    > redim ar(10)
    > for i=0 to 10
    > ar(i)=1
    > next
    > redim preserve ar(4)
    > Response.Write ar(5)
    > Response.End
    > %>
    >
    > If you are using "on error resume next", and you attempt to redim an array
    > that cannot be redim'ed (the only redimmable arrays are those declared

    with
    > the syntax shown in my dim statement above), you may not receive the error
    > message, and the array will be unaffected by the redim statement. That is
    > what happens when I change my code to this:
    >
    > <%
    > on error resume next
    > dim ar(10), i
    > for i=0 to 10
    > ar(i)=i
    > next
    > redim preserve ar(4)
    > for i = 0 to 10
    > Response.Write ar(i) & "<BR>"
    > next
    > Response.End
    > %>
    >
    > Bob Barrows
    > --
    > 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"
    >
    >
     
    Guest, Jan 17, 2004
    #3
    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. Ante Perkovic
    Replies:
    0
    Views:
    622
    Ante Perkovic
    Jul 2, 2003
  2. Nathan Sokalski

    Problem with ReDim statement

    Nathan Sokalski, May 16, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    612
    Charles Law
    May 17, 2005
  3. Duim
    Replies:
    2
    Views:
    305
  4. keyser soze

    REDIM Preserve problem

    keyser soze, May 6, 2008, in forum: ASP General
    Replies:
    4
    Views:
    283
    keyser soze
    May 7, 2008
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    239
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page