我在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最终为您提供最新的值(value)。

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

因此,每次添加新项目时,它将以新的大小更新数组。

关于asp-classic - 动态增加阵列大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8708715/

10-11 14:29