Garbage Collector

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
I read a few article suggesting the use of var = Nothing when the variable is no longer needed. What about in cases like this:
VB.NET:
[color=Blue]Private Sub[/color] Temp()
	[color=Blue]Dim [/color]myString [color=Blue]As String[/color]
	[color=Blue]For[/color] i [color=Blue]As Integer[/color] = 0 [color=Blue]To[/color] 10
		myString = i
	[color=Blue]Next[/color]
	myString = [color=Blue]Nothing[/color]
[color=Blue]End Sub[/color]

To be clear, I am confused whether to call the garbage collector when the code (sub) is actually ending.

Any idea is welcome.
Thanks for helping.
 
the garbage collection is handled by the framework so you dont need to worry about collection

just let variables fall out of scope and everything will be taken care of
 
Dows that means every variable created within a scope need not to be destroyed (var = Nothing) at all? Then, when do we use it; since everything we code is within a scope?

Thanks.
 
when you create a variable and you set it to nothing or you let it fall out of scope, the garbage collector will still only collect it when it's fallen out of scope

in .net you actually dont need to set any variable=nothing, vb really only allows it still as a vb6 compatability
 
hmm thanks for saving my time from typing var = Nothing :)

From what you've said, I think it is almost unnecessary to call the garbage collector manually, unless if I want to dispose a Public variable.
 
There are a few cases in subclassing where it will be necessary to dispose directly of an object, but for the most part, yup, that's about right.

-tg
 
even public variables you could just let fall out of scope, though if you need an application-wide variable (like a public variable) you should declare it as Friend instead of Public

Friend variables can be seen/used anywhere within the application
Public variables can be seen/used anywhere on the system (even other programs)
 
Back
Top