我已经用集合做了你描述的事情。大概是这样的:
- 'local variable to hold collection
- Private mCol As Collection
- Public Function Add(Size As String, Flow As String, Area As String, Station As String, Precip As String, Optional sKey As String) As clsStation
- 'create a new object
- Dim objNewMember As clsStation
- Set objNewMember = New clsStation
- 'set the properties passed into the method
- objNewMember.Flow = Flow
- objNewMember.Area = Area
- objNewMember.Station = Station
- objNewMember.Precip = Precip
- objNewMember.Size = Size
- If Len(sKey) = 0 Then
- mCol.Add objNewMember
- Else
- mCol.Add objNewMember, sKey
- End If
- 'return the object created
- Set Add = objNewMember
- Set objNewMember = Nothing
- End Function
- Public Property Get Item(vntIndexKey As Variant) As clsStation
- 'used when referencing an element in the collection
- 'vntIndexKey contains either the Index or Key to the collection,
- 'this is why it is declared as a Variant
- 'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
- On Error Resume Next
- Set Item = Nothing
- Set Item = mCol(vntIndexKey)
- End Property
- Public Property Get Count() As Long
- 'used when retrieving the number of elements in the
- 'collection. Syntax: Debug.Print x.Count
- Count = mCol.Count
- End Property
- Public Sub Remove(vntIndexKey As Variant)
- 'used when removing an element from the collection
- 'vntIndexKey contains either the Index or Key, which is why
- 'it is declared as a Variant
- 'Syntax: x.Remove(xyz)
- mCol.Remove vntIndexKey
- End Sub
- Public Property Get NewEnum() As IUnknown
- 'this property allows you to enumerate
- 'this collection with the For...Each syntax
- Set NewEnum = mCol.[_NewEnum]
- End Property
- Private Sub Class_Initialize()
- 'creates the collection when this class is created
- Set mCol = New Collection
- End Sub
- Private Sub Class_Terminate()
- 'destroys collection when this class is terminated
- Set mCol = Nothing
- End Sub
我想你可以用一个数组来保存局部变量,并让属性作为变量。 |