If I create an instance of a business class :
Do I have to worry about clearing it out when I am finished with it ?Code:Dim lclsEmployee = New clsEmployee
Printable View
If I create an instance of a business class :
Do I have to worry about clearing it out when I am finished with it ?Code:Dim lclsEmployee = New clsEmployee
No, the garbage thing does that for you.
However, if you are like me, and place code in the terminate event (VB6) of the class then this will annoy you.
The event in VB.NET is the Dispose event.
To run code when the class terminates you must override this dispose event :(
Not too happy with it myself yet.
Woka
I've never used VB 6.o so you've lost me Woka.
Wuff
U've never used VB6?
But you post in the VB 6 section...in fact, what the hell are you doing on the site you deranged Georgie :D
In VB6:
clsEmployee
Then in a form:VB Code:
Private Sub Class_Initialize() MsgBox "Woof" End Sub Private Sub Class_Terminate() MsgBox "Badgers!" End Sub
Does that make sense?VB Code:
Dim objSomeone As clsEmployee Set objSomeone = New clsEmployee 'This cause the initialise event to fire, and a woof msgbox. Set objSomeone = Nothing 'This causes the terminate event to fire, and a badger msgbox.
Anyways, .NET doesn't work like this.
You have new instead of Initialise.
This has more potential as you can do:VB Code:
Public Sub New() 'code here when object is created End Sub
Then in a form you can do:VB Code:
Public Class clsEmployee Public Sub New(ByVal EmployeeID As Long) 'code here to laod Employee from ID End Sub End Class
both lines do essentially the same thing.VB Code:
Dim woof As New clsEmployee(54) Dim woof As clsEmployee = New clsEmployee(67)
Woof
I already know that.
Woof.
I never post to the VB 6.0 forum. You must have me on you're mind a lot you perv badger.;)
Although there is a garbage collector it should not be regarded as not trying to clean up memory. In fact the way the garbage collector works is much like java's garabage collector.
You should use try catch finally blocks and close your db connection as well as release resources in the finally block
try
catch
finally
myConn.close()
myConn = Nothing
You only have to do that with resource intensive objects such as the connection object, image object, etc...Quote:
Originally posted by jhermiz
Although there is a garbage collector it should not be regarded as not trying to clean up memory. In fact the way the garbage collector works is much like java's garabage collector.
You should use try catch finally blocks and close your db connection as well as release resources in the finally block
try
catch
finally
myConn.close()
myConn = Nothing
All others just let them go out of scope, much easier and it really has no impact on performance.
Don't let any of those crazy c++ programers hear you say that, Hellswraith.
Only reason they would have a problem with that is if they don't understand the garbage collection process.Quote:
Originally posted by Magiaus
Don't let any of those crazy c++ programers hear you say that, Hellswraith.
Once a object is allocated in memory, that space isn't freed up unless the garbage collector frees it up. It doesn't matter if you set the reference to null before you leave the method or if you let it drop out of scope, both produce the same action as far as the garbage collector is concerned.
Database connections though are different. They hold references to unmanaged resources. This means you need to call the .Close() methods or .Dispose() methods of those types of objects so the object itself can release the resource it is using immediately instead of waiting around for the garbage collector to come along and free it up by destroying the connection object which is no longer referenced by your app.
Very true. I'll go ahead and mention, just for the heck of it, that if you do doubt this you can call System.GC.Collect(), but it isn't recommened because it requires the extra proccess time.
The only reason I said anything is because I was reminded of a thread with CorneedBee talking about how arrays are so ineficient if you ReDim. You may remember it you explained about creating a ReDim function the allowed you to preserve an existing array. :thumb:
I always trust your advice my friend.
That is scary, since even I don't trust my own advice...lol.Quote:
Originally posted by Magiaus
I always trust your advice my friend.
:D :p