iLogic class destructor

iLogic class destructor

DonStauffer99
Advocate Advocate
687 Views
4 Replies
Message 1 of 5

iLogic class destructor

DonStauffer99
Advocate
Advocate

I created a Finalize method in one of my classes. I instantiate the class in a parameter change rule, so it seems like it should go out of scope after the rule finishes, but the method never executes. Do destructors even work in iLogic classes? Is Finalize the right method name? Thanks!

0 Likes
Accepted solutions (1)
688 Views
4 Replies
Replies (4)
Message 2 of 5

DonStauffer99
Advocate
Advocate

Example: I put this code into a rule, and when it runs, the last message box says than n_ is 3. It seems like it should be zero at that point. The messages in the Finalize method never display. It seems like one or the other of them should display 3 times. I have a bigger project, and classes that unload files in their destructor. Those destructors never ran until I put a Save (to disk) function in my code before the objects go out of scope. Then the destructors mostly started running. Why would it be inconsistent like that?

SyntaxEditor Code Snippet

ublic Class ThisRule
	Public Sub Main()
		Dim a As New Counted
		Dim b As New Counted
		MsgBox("creating a")
		a = Nothing
		MsgBox("a destroyed")
		f()
		MsgBox("f returned")
		b = Nothing
		MsgBox("b destroyed")
		
		MsgBox("n=" & Counted.n_.ToString())
	End Sub
	
	Public Sub f()
		Dim c As New Counted
	End Sub
End Class

Public Class Counted
	Public Shared n_ As Integer = 0
	Public Sub New()
		n_ = n_ + 1
	End Sub
	Public Sub Finalize()
		n_ = n_ - 1
		If n_ = 0 Then
			MsgBox("Destroyed!")
		Else
			MsgBox("Not Destroyed")
		End if
	End Sub
End Class
0 Likes
Message 3 of 5

DonStauffer99
Advocate
Advocate

No speculation, even? Confirmation this happens? Additional information about when it does or doesn't happen, or workarounds?

0 Likes
Message 4 of 5

AlexFielder
Advisor
Advisor
Accepted solution

Hiya,

In my experience, in a "Try Catch Finally" scenario - the Finally always fires so you could use that instead?

Message 5 of 5

DonStauffer99
Advocate
Advocate

That's an interesting idea. I did discover that Microsoft revised VB so it no longer has destructors! This may help work around. Thanks .for the suggestion.

0 Likes