|
-
Feb 10th, 2004, 11:45 AM
#1
ErrorHandling...Try, Catch and Finally...easy question.
OK, I have the following:
VB Code:
Public Sub Fetch()
Dim Svr As UserSvr 'This is my own created data access class
Dim DS As DataSet
Dim Row As DataRow
Try
Svr = New UserSvr
DS = Svr.Fetch
For Each Row In DS.Tables.Item("Users").Rows
Add(CStr(Row.Item("Username")), CStr(Row.Item("Email")), CStr(Row.Item("Telephone")), False) 'Cbln(Row.Item("Administrator")))
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
Row = Nothing
Svr = Nothing
DS = Nothing
End Try
End Sub
Now I am using a DataSet here passed up from the DA Layer, but that's irrelevant here.
Now. In the FINALLY section of the Try thingy I clean up my objects, which seems fair enough. However, while I am populating my classes with data from the dataset, in the For Next loop, I still have a reference to to server DA object (SVR)...So should my code be modified to:
[VBOCDE]
Try
Svr = New UserSvr
DS = Svr.Fetch
Svr = Nothing
[/Highlight]
So I have the line of code:
twice...once in the TRY section and once in the FINALLY section
Although doing the aboce still keeps it open coz of garbage collection....what would you do here?
Woof
-
Feb 10th, 2004, 11:55 AM
#2
Actually the GC will automatically detect when there are no more references (because of scope) to an object In this case that is right after the method finishes so I wouldn't even bother setting anything to Nothing just make sure any connections are closed. This can take some getting used to coming from VB6 and I suppose there is nothing wrong with using nothing but in most cases its no longer needed.
-
Feb 10th, 2004, 12:35 PM
#3
Id watch out for large objects though. The GC wont collect them unless its running outta memory or is really bored, so you still have them in memory, even though you cant access them.
M$ have a paper on the GC and its a very interesting read (edit, I cant find the one I have here, but this is an OK read):
http://msdn.microsoft.com/library/de...etgcbasics.asp
and while im here, this is also quite useful:
http://msdn.microsoft.com/library/de...etperftips.asp
-
Feb 10th, 2004, 02:43 PM
#4
Registered User
i think that garbage collect just designates the variable as unused ram so that when the system decides it needs more it is deleted. it dosnt actually clear it itself.
-
Feb 10th, 2004, 03:46 PM
#5
Thats what I just said .
-
Feb 10th, 2004, 08:30 PM
#6
Registered User
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|