Tôi có mảng này trong ASPTăng Mảng Kích động
CONST CARTPID = 0
CONST CARTPRICE = 1
CONST CARTPQUANTITY = 2
dim localCart(3,20)
tôi thêm các mục vào mảng này tự động như thế này
localCart(CARTPID,i) = productId
localCart(CARTPRICE,i) = productPrice
localCart(CARTPQUANTITY,i) = 1
Vấn đề là, sau 4 mục, tôi vẫn có thể thêm các mục nhưng UBound luôn luôn trở lại 3. Điều đó gây ra tình trạng của tôi thất bại.
Tôi muốn tăng kích cỡ của mảng này tại thời gian chạy để UBOUND có thể trả về giá trị mới nhất.
Vui lòng cho tôi biết cách tôi có thể làm điều đó. Đây là mã hoàn chỉnh của tôi
'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
Cảm ơn, công trình này nhưng tôi không thể bảo tồn các giá trị. Ngay sau khi tôi sử dụng ReDim Preserve, tôi nhận được lỗi này: Microsoft VBScript runtime error '800a0009' subscript ra khỏi phạm vi /prices_test.asp, dòng 102 – VJV
hiển thị mã của bạn để chúng ta có thể nhìn thấy nơi các lỗi đang xảy ra – Robert
nhờ , Tôi đã thêm mã hoàn chỉnh vào thư gốc của mình. – VJV