Do I need to delete the class?
-
I am writing an application which should have a whole bunch of classes. Depending upon the choices of the user, some of the classes will be actually used and others will not be used. Now I'm worried about garbage collection, since Visual Basic isn't Java. Is my code below mistaken in that I need to call delete?
Private Sub Test1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1.Click Dim myScope As ScopeClass = New ScopeClass Dim stat As Integer stat = myScope.Init(Me) stat = myScope.PerformScan(1) myScope.Close() End Sub
If I do call a series of classes and I go from one class to another, is there an easy way to have a pointer to the last class I used so that I don't have to know specifically what it was at the point when I want to delete it? If I can do this, please suggest a code snippet with the proper syntax to do it. Thanks, Ilan -
I am writing an application which should have a whole bunch of classes. Depending upon the choices of the user, some of the classes will be actually used and others will not be used. Now I'm worried about garbage collection, since Visual Basic isn't Java. Is my code below mistaken in that I need to call delete?
Private Sub Test1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1.Click Dim myScope As ScopeClass = New ScopeClass Dim stat As Integer stat = myScope.Init(Me) stat = myScope.PerformScan(1) myScope.Close() End Sub
If I do call a series of classes and I go from one class to another, is there an easy way to have a pointer to the last class I used so that I don't have to know specifically what it was at the point when I want to delete it? If I can do this, please suggest a code snippet with the proper syntax to do it. Thanks, IlanIlanTal wrote:
Now I'm worried about garbage collection, since Visual Basic isn't Java.
The .NET framework has automatic garbage collection and memory management. You can also use the Dispose() and Finalize() methods of classes. Suggestion for your code - declare your class and counter variables at modular level and instantiate the class when your chosen event is fired e.g.
Option Strict On
....
'module level class declaration
Dim myScope as ScopeClass'module level counter variable
Dim stat as integerPrivate Sub Test1_Click(....) Handles Test1.Click
myScope = New ScopeClass
stat +=1....
End Sub
Remember to decrement your counter variable if/when you dispose the class (
stat -=1
). -
I am writing an application which should have a whole bunch of classes. Depending upon the choices of the user, some of the classes will be actually used and others will not be used. Now I'm worried about garbage collection, since Visual Basic isn't Java. Is my code below mistaken in that I need to call delete?
Private Sub Test1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1.Click Dim myScope As ScopeClass = New ScopeClass Dim stat As Integer stat = myScope.Init(Me) stat = myScope.PerformScan(1) myScope.Close() End Sub
If I do call a series of classes and I go from one class to another, is there an easy way to have a pointer to the last class I used so that I don't have to know specifically what it was at the point when I want to delete it? If I can do this, please suggest a code snippet with the proper syntax to do it. Thanks, IlanIlanTal wrote:
what it was at the point when I want to delete it?
Delete it?? You don't have to delete it. If the instance goes out of scope and there are no references to it, it gets killed automatically.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
IlanTal wrote:
Now I'm worried about garbage collection, since Visual Basic isn't Java.
The .NET framework has automatic garbage collection and memory management. You can also use the Dispose() and Finalize() methods of classes. Suggestion for your code - declare your class and counter variables at modular level and instantiate the class when your chosen event is fired e.g.
Option Strict On
....
'module level class declaration
Dim myScope as ScopeClass'module level counter variable
Dim stat as integerPrivate Sub Test1_Click(....) Handles Test1.Click
myScope = New ScopeClass
stat +=1....
End Sub
Remember to decrement your counter variable if/when you dispose the class (
stat -=1
).Thanks for your suggestions. Question: My Test1_Click is a button, so every time I press the button I get a new instance of the class? (According to the code it should be so.) When it goes out of scope does the instance hang around in memory? It seems your use of stat is a counter, but I'm only incrementing it each time I click the button. Should I use Dispose() at the end of the button routine? Is this something I need to consider, or can I just forget it like in Java?
-
Thanks for your suggestions. Question: My Test1_Click is a button, so every time I press the button I get a new instance of the class? (According to the code it should be so.) When it goes out of scope does the instance hang around in memory? It seems your use of stat is a counter, but I'm only incrementing it each time I click the button. Should I use Dispose() at the end of the button routine? Is this something I need to consider, or can I just forget it like in Java?
IlanTal wrote:
When it goes out of scope does the instance hang around in memory?
No, if it goes out of scope the instance is no more and the memory is reclaimed.
IlanTal wrote:
Should I use Dispose() at the end of the button routine?
You can call the Dispose() method when you no longer need your class. If you call it at the end of your button routine you'll throw a null reference exception if you try to make use of the class later in another sub as you cannot access a disposed object. A basic use of the Dispose() method in your click event would be something like
Option Strict On
Public Class Form1Dim myScope as ScopeClass 'declare the class
Private Sub Test_Click(...)
'create new ScopeClass instance
myScope = New ScopeClass'do whatever the ScopeClass needs to do
'call other methods/functions etc
......'ScopeClass is no longer needed,
'all processing that needed to be done is complete
myScope.Dispose()End Sub
End ClassBecause of the automatic garbage collection of the .NET framework you don't HAVE to call Dispose() but it can be used to manually reclaim memory.