Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:49:39+00:00 2026-06-01T07:49:39+00:00

In this VB.NET code: Dim o as SomeClass Try o = new SomeClass ‘call

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:49:41+00:00Added an answer on June 1, 2026 at 7:49 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this in some vb.net source code: Dim sTest As String = &
Here is my code Dim dt As DataTable = New DataTable(Intervenant) For Each column
I've got some problems. I've got this code: Dim infoID As Integer = objCommand1.ExecuteScalar()
I have this code: Dim Tasks As New List(Of Task) Tasks.Add(Task.Factory.StartNew(Sub() 'Do whatever End
Is there an array literal in VB.NET 9/ VS2008? Something like this pseudo-code: Dim
I have this code: Dim StringParts As New List(Of String)(OriginalString.Split(New Char() {\c}, StringSplitOptions.RemoveEmptyEntries)) When
example: http://jsfiddle.net/kuTLf/ code looks like this: <div id=main> <div id=slideshow class=pics> <div id=nav></div> <img
This is my code: http://jsfiddle.net/WCP5f/2/ If you open it with Chrome, you will see
This is my fiddle code http://jsfiddle.net/coderslay/WnC9B/44/ It is successfully plotting the graph. The problem
This line of code ((Matches)Container.DataItem).MatchID works in C# but in VB.NET, when used in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.