我正在尝试在Classic ASP中创建一个类,并且已经接近我在寻找的东西,但是还远远不够。我还没有找到解决我问题的例子。
在示例中,我需要将LineItem类作为数组存储在Order类中,以便一个订单可以有多个订单项。当前它可以使用,但仅允许LineItem的单个实例。有人可以给我一个例子,向我展示如何使其成为数组吗?我了解如何创建对象数组,但不了解如何将对象存储在父对象中。我很抱歉,但是自从我用Classic ASP进行任何编程以来已经15年了,我已经忘记了那时的大部分知识。
<%
Class LineItem
Private LineNumberVar
Private ItemCostVar
Private QuantityVar
Private ItemCodeVar
Private ItemDescriptionVar
' Line Number
Public Property Get LineNumber()
LineNumber = LineNumberVar
End Property
Public Property Let LineNumber(lineNumberParam)
LineNumberVar = lineNumberParam
End Property
' Item Cost
Public Property Get ItemCost()
ItemCost = ItemCostVar
End Property
Public Property Let ItemCost(itemCostParam)
ItemCostVar = itemCostParam
End Property
' Quantity
Public Property Get Quantity()
Quantity = QuantityVar
End Property
Public Property Let Quantity(quantityParam)
QuantityVar = quantityParam
End Property
' Item Code
Public Property Get ItemCode()
ItemCode = ItemCodeVar
End Property
Public Property Let ItemCode(itemCodeParam)
ItemCodeVar = itemCodeParam
End Property
' Item Description
Public Property Get ItemDescription()
ItemDescription = ItemDescriptionVar
End Property
Public Property Let ItemDescription(itemDescriptionParam)
ItemDescriptionVar = itemDescriptionParam
End Property
End Class
Class Order
Private CustomerIDVar
Private OrderIDVar
Private EmailVar
Private ItemsVar
'CustomerID
Public Property Get CustomerID()
CustomerID = CustomerIDVar
End Property
Public Property Let CustomerID(customerIDParam)
CustomerIDVar = customerIDParam
End Property
'OrderID
Public Property Get OrderID()
OrderID = OrderIDVar
End Property
Public Property Let OrderID(orderIDParam)
OrderIDVar = orderIDParam
End Property
'Email
Public Property Get Email()
Email = EmailVar
End Property
Public Property Let Email(emailParam)
EmailVar = emailParam
End Property
' Line Items (needs to be an array of LineItem class objects)
'Items
Public Property Get Items()
Set Items = ItemsVar
End Property
' must use Set for objects
Public Property Set Items(itemsParam)
Set ItemsVar = itemsParam
End Property
Private Sub Class_Initialize
ItemsVar = Array() ' initialize to empty dynamic array
End Sub
Function AddLineItem(itemArray, item)
ReDim Preserve itemArray(UBound(itemArray) + 1)
itemArray(UBound(itemArray)) = items
AddLineItem = itemArray
End Function
End Class
Dim ItemsVar
Set ItemsVar = new LineItem
ItemsVar.LineNumber = 1
ItemsVar.ItemCost = 25.00
ItemsVar.Quantity = 1
ItemsVar.ItemCode = "42667"
ItemsVar.ItemDescription = "Software"
Dim OrderVar
Set OrderVar = New Order
Set OrderVar.Items = ItemsVar
OrderVar.CustomerID = "0937263"
OrderVar.OrderID = "2348725"
OrderVar.Email = "nobody@gmail.com"
%>
<%= OrderVar.CustomerID %><br />
<%= OrderVar.OrderID %><br />
<%= OrderVar.Email %><br />
<%= OrderVar.Items.LineNumber %><br />
<%= OrderVar.Items.ItemCost %><br />
<%= OrderVar.Items.Quantity %><br />
<%= OrderVar.Items.ItemCode %><br />
<%= OrderVar.Items.ItemDescription %><br />
答案 0 :(得分:1)
由于只需要向数组添加一个行项,就可以使AddLineItem
成为子而不是函数。
Class Order
Private ItemsVar
Public Property Get Items()
Items = ItemsVar
End Property
Public Property Set Items(itemsParam)
Set ItemsVar = itemsParam
End Property
Private Sub Class_Initialize
ItemsVar = Array()
End Sub
Sub AddLineItem(newitem)
ReDim Preserve ItemsVar(UBound(ItemsVar) + 1)
set ItemsVar(UBound(ItemsVar)) = newitem
End Sub
End Class
要添加新的订单项,
Set OrderVar = New Order
Ordervar.AddLineItem(NewItem)
要遍历订单项,
For each ThisLineItem in OrderVar.Items
Response.write ThisLineItem.ItemDescription & "<br>"
Next