It sounds like you're trying to reset your objects to their default state, effectively simulating deletion by reinitializing them. In VBA (and twinBASIC), setting an object variable to Nothing only removes that reference to the object—it doesn't affect other references to that object. Also, calling Class_Terminate directly doesn't force the object to be destroyed or reset; it's a special method that's called automatically when the object is truly released (i.e., when all references to it are gone).
From your code and description, the main issue is that setting local variables to Nothing or calling Class_Terminate isn't resetting the actual state of the objects or removing all references to them. To reset your objects to their default state, you need to explicitly reset their internal state—usually by creating a Reset method that sets their internal variables back to default values.
Here's how you can modify your code to achieve the desired behavior:
-
Add a Reset method to your objVal class that resets its internal variables.
-
Modify your Purge method in objMgr to call this Reset method on each object.
-
Ensure all references to the objects are properly managed so that once you reset or remove them, they don't linger due to other references.
Below is the modified code with explanations:
Modified objVal Class:
Class objVal
Public __val__ As Variant
Private __metaVal__ As metaVal
Sub Class_Initialize()
Set __metaVal__ = New metaVal(Me)
objMgr.Add __metaVal__
End Sub
Public Sub AddDerived(ByRef derivedVal As objVal, ByRef key As String)
__metaVal__.AddDerived derivedVal, key
End Sub
Public Sub AddDerived(ByRef derivedVal As Variant, ByRef key As String)
objMgr.InhibitNext "Population of derived member [key=" & key & "] to ObjMgr collection prevented"
Dim derived As New objVal
derived.[__val__] = derivedVal
__metaVal__.AddDerived derived, key
End Sub
[DefaultMember]
Public Property Get Val() As Variant
Val = __val__
End Property
[DefaultMember]
Public Property Let Val(val As Variant)
__val__ = val
End Property
' New Reset method to reset the object's state
Public Sub Reset()
__val__ = Empty
If Not __metaVal__ Is Nothing Then
__metaVal__.Reset
End If
End Sub
End Class
Modified metaVal Class:
Class metaVal
Public __public__ As objVal
Private __topLevel__ As objVal
Private __derived__ As objValColl
Sub Class_Initialize()
Set __derived__ = New objValColl
End Sub
Sub New(ByRef objVal As objVal)
Set __public__ = objVal
Set __topLevel__ = objVal
End Sub
Public Sub AddDerived(ByRef objVal As objVal, key As String)
If __derived__ Is Nothing Then Set __derived__ = New objValColl
__derived__.Add objVal, key
End Sub
Public Sub SetPublicTopLevel()
Set __public__ = __topLevel__
End Sub
Public Sub SetPublicDerived(ByRef key As String)
Set __public__ = __derived__.Get(key)
End Sub
' New Reset method to reset the metaVal and its associated objects
Public Sub Reset()
If Not __public__ Is Nothing Then
__public__.__val__ = Empty
End If
If Not __topLevel__ Is Nothing Then
__topLevel__.__val__ = Empty
End If
If Not __derived__ Is Nothing Then
__derived__.ResetAll
End If
End Sub
End Class
Modified objValColl Class:
Class objValColl
Private __coll__ As New Collection
Public Sub Add(ByRef objVal As objVal, ByRef key As String)
__coll__.Add objVal, key
End Sub
Public Function Get(ByRef key As String) As objVal
Set Get = __coll__.Item(key)
End Function
' New method to reset all objVal instances in the collection
Public Sub ResetAll()
Dim obj As objVal
For Each obj In __coll__
obj.Reset
Next obj
End Sub
End Class
Modified objMgr Class:
[PredeclaredId]
Class objMgr
Private __coll__ As New Collection
Public Sub Add(ByRef managedVal As metaVal)
If __inhibit__ Then Exit Sub
__coll__.Add managedVal
End Sub
Public Sub InhibitNext(Optional msg As String)
__inhibit__ msg, True
End Sub
Private Function __inhibit__(Optional msg As Variant, Optional inhibitNext As Variant) As Variant
Static __inhibit_next__ As Variant
Static __msg__ As Variant
If Not IsMissing(msg) Then __msg__ = msg
If Not IsMissing(inhibitNext) Then __inhibit_next__ = inhibitNext
If Not IsMissing(msg) Or Not IsMissing(inhibitNext) Then Exit Function
If __inhibit_next__ = True Then
Debug.Print __msg__
__msg__ = Empty
__inhibit_next__ = Empty
__inhibit__ = True
Else
__inhibit__ = False
End If
End Function
' Modified Purge method
Public Sub Purge()
Dim item As metaVal
For Each item In __coll__
item.Reset
Next item
' Optionally clear the collection if you want to remove all items
' Set __coll__ = New Collection
End Sub
End Class
In the modified Purge method of objMgr, we now call the Reset method on each metaVal object, which in turn resets their associated objVal instances. This approach ensures that the state of all objects is reset, reflecting the changes in all references to those objects.
Notes:
-
Resetting Objects vs. Deleting Objects: In VBA, if you want to simulate deleting an object, you need to remove all references to it (i.e., set all variables referencing it to Nothing). However, if you want to keep the object but reset its state, you should provide a method (like Reset) that reinitializes its internal variables.
-
Setting Variables to Nothing: Setting a local variable to Nothing only affects that variable—it doesn't affect other references to the same object. If you have multiple references to an object, you need to manage all of them.
-
The Class_Terminate Method: You can't directly call Class_Terminate to destroy an object. It's a special method that's automatically called when an object is about to be destroyed (i.e., when there are no more references to it). Trying to call it manually won't have the desired effect.
-
Collections and References: Objects stored in collections maintain references. Removing an item from a collection reduces the reference count by one. If other variables reference the same object, it won't be destroyed until all references are gone.
Test the Modified Code:
Now, when you run your test subroutine, calling objMgr.Purge will reset the state of all managed objects:
Sub test()
Dim objA As New objVal
Dim objB As New objVal
' Create objects and set values
objA.[__val__] = 5
objB.[__val__] = "kaas"
' Change values
objA.[__val__] = 555
' Use default member behavior
objB = "lekkerrrr"
objB.AddDerived "smerig man", "B"
objB.AddDerived "kinda average", "C"
' Display values before purge
Debug.Print "Before Purge:"
Debug.Print "objA.__val__ = " & objA.__val__
Debug.Print "objB.__val__ = " & objB.__val__
' Purge objects
objMgr.Purge
' Display values after purge
Debug.Print "After Purge:"
Debug.Print "objA.__val__ = " & objA.__val__
Debug.Print "objB.__val__ = " & objB.__val__
End Sub
Expected Output:
Before Purge:
objA.__val__ = 555
objB.__val__ = lekkerrrr
After Purge:
objA.__val__ =
objB.__val__ =
As you can see, after calling objMgr.Purge, the __val__ properties of objA and objB are reset to empty values.
Conclusion:
By adding Reset methods to your classes and ensuring that all internal state variables are reset, you can effectively return your objects to their default state. This approach maintains the object instances but resets their state, which seems to align with your goal.
Let me know if you have any questions or need further assistance!