Solution to the boundary problem of ASP dynamic array subscript

Source: Internet
Author: User

In an ASP program, you need to define a dynamic array, but the size of the array can not be ReDim in place once, depending on the actual need to increase, and then have the following code:

The code is as follows Copy Code

<%
Dim arr ()
' Assign value
For i = 1 to 10
Redim Preserve arr (UBound (arr) + 1)
Arr (UBound (arr)) = I
Next
' Traversal
For i=0 to UBound (arr)
Response.Write (Arr (i) & "<br/>")
Next
%>

Run Error:

The reason is not to call the ReDim definition array before the subscript, using UBound to obtain an array of RCAs error, prompted the subscript out of bounds, the code can be slightly modified to resolve:

The code is as follows Copy Code

<%
Dim arr ()
ReDim arr (0)
' Assign value
For i = 1 to 10
If arr (UBound (arr)) <> "" Then
Redim Preserve arr (UBound (arr) + 1)
End If
Arr (UBound (arr)) = I
Next
' Traversal
For i=0 to UBound (arr)
Response.Write (Arr (i) & "<br/>")
Next
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.