类中的经典ASP arraylist属性?

时间:2012-09-19 16:03:51

标签: class asp-classic arraylist

如何使用经典ASP将arraylist属性添加到类中?

我的例子(不工作):

Class AA
    Public AA_id
    Dim data: set data = CreateObject("System.Collections.ArrayList")
End Class

1 个答案:

答案 0 :(得分:0)

您需要在类构造函数的方法 - 函数,子例程中初始化类成员。当然,其中最好的是在构造函数中。

完全正常运行的代码,演示如何将.NET ArrayList用作自定义VBScript类的一部分:

<%
Class MyCustomClass
    Private internalArrayList

    Private Sub Class_Initialize()  
        Set internalArrayList = Server.CreateObject("System.Collections.ArrayList")
    End Sub  

    Private Sub Class_Terminate()  
        Set internalArrayList = Nothing
    End Sub  

    Public Sub AddItem(oItem)
        internalArrayList.Add(oItem)
    End Sub

    Public Function ToString()
        Dim data, x
        data = "List contains " & internalArrayList.Count & " item"
        If internalArrayList.Count<>1 Then data = data & "s"
        For x=0 To internalArrayList.Count-1
            data = data & "<br />"
            If IsObject(internalArrayList(x)) Then
                data = data & "complex object of type " & TypeName(internalArrayList(x))
            ElseIf IsArray(internalArrayList(x)) Then
                data = data & "array with " & (UBound(internalArrayList(x)) + 1) & " items"
            Else  
                data = data & internalArrayList(x)
            End If
        Next
        ToString = data
    End Function
End Class
%>

用法示例,包括添加复杂对象:

Class Foo
    Public Dummy
End Class

Dim myInstance, myFoo
Set myInstance = New MyCustomClass
myInstance.AddItem("hello")
myInstance.AddItem("world")
myInstance.AddItem(Array(1, 2, 3))

Set myFoo = New Foo
myFoo.Dummy = "just checking"
myInstance.AddItem(myFoo)

Response.Write(myInstance.ToString())