1

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

我的例子(不工作):

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

1 回答 1

1

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

演示如何将 .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())
于 2012-09-20T06:56:25.517 回答