如何让 class 对象实例在 Lotus Script 中删除自身?
How to get a class object instance to delete itself in Lotus Script?
Lotus 脚本通常不允许 class 调用它自己的 Delete() 过程。
只有当引用对象的代码得到所有这些未使用的引用时,垃圾收集器才会将其删除。
有解决办法吗?
例如:
Class MySuicidalClass
Sub New()
End Sub
Sub Delete()
End Sub
Public Sub KillMyself()
Call Me.Delete() 'Error: Illeagal call to Delete
Delete Me 'Error: Variable required (Me)
End Sub
End Class
使用删除对象的外部过程,class 实例可以调用它来杀死自己。
这有效地将所有对对象实例的引用设置为 Nothing。
像这样:
'In the module level:
Private Sub DeleteObjectInstance(Obj As Variant)
Delete Obj
End Sub
Class MySuicidalClass
Sub New()
End Sub
Sub Delete()
End Sub
Public Sub KillMyself()
Call DeleteObjectInstance(Me) 'This works!
End Sub
End Class
Sub Test
Dim sc As MySuicidalClass
Set sc = New MySuicidalClass
Call sc.KillMyself()
MsgBox "sc is Nothing = " & CStr(sc Is Nothing) 'shows: TRUE
End Sub
Lotus 脚本通常不允许 class 调用它自己的 Delete() 过程。
只有当引用对象的代码得到所有这些未使用的引用时,垃圾收集器才会将其删除。
有解决办法吗?
例如:
Class MySuicidalClass
Sub New()
End Sub
Sub Delete()
End Sub
Public Sub KillMyself()
Call Me.Delete() 'Error: Illeagal call to Delete
Delete Me 'Error: Variable required (Me)
End Sub
End Class
使用删除对象的外部过程,class 实例可以调用它来杀死自己。
这有效地将所有对对象实例的引用设置为 Nothing。
像这样:
'In the module level:
Private Sub DeleteObjectInstance(Obj As Variant)
Delete Obj
End Sub
Class MySuicidalClass
Sub New()
End Sub
Sub Delete()
End Sub
Public Sub KillMyself()
Call DeleteObjectInstance(Me) 'This works!
End Sub
End Class
Sub Test
Dim sc As MySuicidalClass
Set sc = New MySuicidalClass
Call sc.KillMyself()
MsgBox "sc is Nothing = " & CStr(sc Is Nothing) 'shows: TRUE
End Sub