3

In this VB.NET code:

Dim o as SomeClass
Try
   o = new SomeClass
   'call some method on o here
Catch(...)
   ...
Finally
   o = Nothing
End Try

Why is there a need to set o to Nothing? What if i don't set it to Nothing in the Finally block? What i think is that it is OK if you don't set it to Nothing because the object will be marked for GC.

badmaash
  • 4,775
  • 7
  • 46
  • 61
  • If SomeClass has a .Dispose method, you should use that: http://stackoverflow.com/questions/12368/how-to-dispose-a-class-in-net – Andrew Morton Apr 04 '12 at 18:18
  • possible duplicate of [Calling null on a class vs Dispose()](http://stackoverflow.com/questions/574019/calling-null-on-a-class-vs-dispose) (Ignore the fact that the top-rated answer is in C#. It's exactly the same for VB.NET. Setting an object to `Nothing` is *completely superfluous* in VB.NET. Note that's different from the COM-based versions of VB like VB 6.) – Cody Gray - on strike Apr 05 '12 at 02:24

2 Answers2

4

If the object is unsafe to use out of the try catch should this be done. If this was a stream for example you'd see the stream closed and then set to nothing. It is not always the right thing to do but this code is seen a lot.

consider this code

Sub Main()
    Dim o As String
    Try
        o = "Hello"
        Console.Out.WriteLine("hi {0}", o)
    Catch ex As Exception
        ' do something here
    Finally
        o = Nothing
    End Try

    ' unable to do something here
End Sub

Allthough it is a silly example it does mean you cannot reference o outside now because it is no longer set to an instance of an object. That is why it is done by a lot of people. If you are in a function and a function ends at that point there is no need to set to Nothing as the object falls out of scope however lots of people will set stuff to Nothing out of habit I'd consider that incorrect and bad code design

krystan honour
  • 6,523
  • 3
  • 36
  • 63
  • Does that mean, as a practice, i should set all objects to `Nothing` once i am done with them? – badmaash Apr 04 '12 at 10:37
  • 1
    @badmaash: No. As krystan said "If the object is unsafe to use" only. The vast majority of the time it is not needed. – Meta-Knight Apr 04 '12 at 15:06
  • 1
    Any class that's correctly designed will throw an exception if you try to use it after it has been disposed. It's superfluous to set it to `Nothing`. And I'd argue it's a bad pattern to get into because people think it actually does something. It just adds noise to your code. And, as you mention, if the class *is* poorly designed such that it *doesn't* throw an exception upon improper usage, you should definitely use scoping to your advantage. A `Using` statement is the easiest thing, but you can also wrap it in a new function. – Cody Gray - on strike Apr 05 '12 at 02:22
0

It's because the object is not safe to use outside the the try.. catch.. finally block. It's not guaranteed it's in a consistent state, so it's set to Nothing to make it obvious t's not supposed to be used.

Vlad Ciobanu
  • 1,473
  • 1
  • 11
  • 11
  • 3
    Actually. no. That's a holdover from VB6 and COM days, when setting to `Nothing` actually decremented the reference count on the object. – John Saunders Apr 04 '12 at 10:36