加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp-classic – 动态增加数组大小

发布时间:2020-12-16 07:04:19 所属栏目:asp.Net 来源:网络整理
导读:我在ASP中有这个数组 CONST CARTPID = 0CONST CARTPRICE = 1CONST CARTPQUANTITY = 2dim localCart(3,20) 我像这样动态地向这个数组添加项目 localCart(CARTPID,i) = productIdlocalCart(CARTPRICE,i) = productPricelocalCart(CARTPQUANTITY,i) = 1 问题是,
我在ASP中有这个数组

CONST CARTPID = 0
CONST CARTPRICE = 1
CONST CARTPQUANTITY = 2
dim localCart(3,20)

我像这样动态地向这个数组添加项目

localCart(CARTPID,i) = productId
localCart(CARTPRICE,i) = productPrice
localCart(CARTPQUANTITY,i) = 1

问题是,在4个项目之后,我仍然可以添加项目,但UBound总是返回3.这导致我的条件失败.

我想在运行时增加此数组的大小,以便UBOUND可以返回最新值.

请让我知道我该怎么做.这是我的完整代码

'Define constants
 CONST CARTPID = 0
 CONST CARTPRICE = 1
 CONST CARTPQUANTITY = 2

 'Get the shopping cart.
 if not isArray(session("cart")) then
dim localCart(3,20)
 else
localCart = session("cart")
 end if

 'Get product information
 productID = trim(request.QueryString("productid"))
 productPrice = trim(request.QueryString("price"))

 'Add item to the cart

 if productID <> "" then
foundIt = false
for i = 0 to ubound(localCart)
    if localCart(CARTPID,i) = productId then
        localCart(CARTPQUANTITY,i) = localCart(CARTPQUANTITY,i)+1
        foundIt = true
        exit for
    end if
next
if not foundIt then
    for i = 0 to 20

        if localCart(CARTPID,i) = "" then
                            ***ReDim Preserve localCart(UBound(localCart,1) + 1,20)***
            localCart(CARTPID,i) = productId
            localCart(CARTPRICE,i) = productPrice
            localCart(CARTPQUANTITY,i) = 1
            exit for
        end if
    next
end if
 end if

解决方法

我认为在每次添加新项目后使用当前UBound 1重新编写数组将使UBound最终为您提供最新值.

// New item addition code will go here
ReDim localCart(UBound(localCart,20)

因此,每次添加新项目时,它都会使用新大小更新阵列.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读