Results 1 to 6 of 6

Thread: ErrorHandling...Try, Catch and Finally...easy question.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Unhappy ErrorHandling...Try, Catch and Finally...easy question.

    OK, I have the following:
    VB Code:
    1. Public Sub Fetch()
    2.         Dim Svr As UserSvr 'This is my own created data access class
    3.         Dim DS As DataSet
    4.         Dim Row As DataRow
    5.         Try
    6.             Svr = New UserSvr
    7.             DS = Svr.Fetch
    8.             For Each Row In DS.Tables.Item("Users").Rows
    9.                 Add(CStr(Row.Item("Username")), CStr(Row.Item("Email")), CStr(Row.Item("Telephone")), False) 'Cbln(Row.Item("Administrator")))
    10.             Next
    11.         Catch ex As Exception
    12.             MsgBox(ex.Message)
    13.         Finally
    14.             Row = Nothing
    15.             Svr = Nothing
    16.             DS = Nothing
    17.         End Try
    18.     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:
    VB Code:
    1. Svr = Nothing
    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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

  4. #4
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    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.

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Thats what I just said .

  6. #6
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    good. im happy we agree.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width