Hi. I have this module class where one of the properties is a collection. At some points I want to be able to do the following to my class:
Either I misunderstand the way collections work or I have a really stupid mistake in my code I'm overlooking the whole time. Of course I could work my way around this problem, but I still would not understand what is going wrong here. Help would be mutch appriciated.
Class module:
VBA Module:
- fix the state of collection1 (stor it's content into collection2)
- do things with collection1
- reset collection1 to the value of collection2
Class module:
Code:
Option Compare Database
Private cltActiveReports As Collection
Private cltActiveReportsReset As Collection
''''''''''''''''''''''''''''''''''''''''''''''
' cltActiveReports property
''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get ActiveReportsCollection() As Collection
Set ActiveReportsCollection = cltActiveReports
End Property
Public Property Let ActiveReportsCollection(cltNewActiveReports As Collection)
Set cltActiveReports = Nothing
Set cltActiveReports = New Collection
Set cltActiveReports = cltNewActiveReports
End Property
Public Property Let AddActiveReport(Value As String)
cltActiveReports.Add Value
End Property
''''''''''''''''''''''''''''''''''''''''''''''
' cltActiveReportsReset property
''''''''''''''''''''''''''''''''''''''''''''''
Public Property Get ActiveReportsCollectionReset() As Collection
Set ActiveReportsCollectionReset = cltActiveReportsReset
End Property
Public Property Let ActiveReportsCollectionReset(cltNewActiveReports As Collection)
Set cltActiveReportsReset = Nothing
Set cltActiveReportsReset = New Collection
Set cltActiveReportsReset = cltNewActiveReports
End Property
' initialize class: set default values
Private Sub Class_Initialize()
Set cltActiveReports = New Collection
Set cltActiveReportsReset = New Collection
End Sub
''''''''''''''''''''''''''''''''''''''''''''''
' SetResetPositionForm function
''''''''''''''''''''''''''''''''''''''''''''''
Public Sub SetResetPositionForm()
Me.ActiveReportsCollectionReset = Me.ActiveReportsCollection
End Sub
''''''''''''''''''''''''''''''''''''''''''''''
' ResetFormClass function
''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ResetFormClass()
Me.ActiveReportsCollection = Me.ActiveReportsCollectionReset
End Sub
Code:
Option Compare Database
Public Function testClt()
' Setup the form handler to store filtering and source data into
Dim clsIBForm As New Class1
clsIBForm.SetResetPositionForm
Debug.Print "active reports 1:" & clsIBForm.ActiveReportsCollection.Count
Debug.Print "reset reports 1:" & clsIBForm.ActiveReportsCollectionReset.Count
clsIBForm.AddActiveReport = "Report1"
' This is the point were I would expect only the cltActiveReports property to be changed,
' but unfortunately cltActiveReportsReset appears to have changed as well.
Debug.Print "active reports 2:" & clsIBForm.ActiveReportsCollection.Count
Debug.Print "reset reports 2:" & clsIBForm.ActiveReportsCollectionReset.Count
clsIBForm.ResetFormClass 'does not work unforyunately due to the changed cltActiveReportsReset collection
Debug.Print "active reports 3:" & clsIBForm.ActiveReportsCollection.Count
Debug.Print "reset reports 3:" & clsIBForm.ActiveReportsCollectionReset.Count
End Function