I have this array in ASP
CONST CARTPID = 0
CONST CARTPRICE = 1
CONST CARTPQUANTITY = 2
dim localCart(3,20)
I add items to this array dynamically like this
localCart(CARTPID,i) = productId
localCart(CARTPRICE,i) = productPrice
localCart(CARTPQUANTITY,i) = 1
The problem is, after 4 items, I can still add the items but UBound always return 3. Which causing my conditions failing.
I want to increase size of this array at run time so that UBOUND can return latest value.
Please let me know how can i do that. Here is my complete code
'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
I think redimentioning the array with the current UBound+1 after each addition of new item will make UBound gives you the latest value finally.
So it will update your array with the new size every time you will add the new item.