Results 1 to 7 of 7

Thread: setting objects to nothing

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    setting objects to nothing

    Hey,

    If I have an object, how can I set it to nothing?
    I want to be able to check with is nothing.
    Calling the dispose method doesn't set the object to nothing. I want the object to be in the same state as when I was declared.

    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: setting objects to nothing

    Depending on the object you can do a .Close that also calls the .Dispose method.
    Also, actually setting the object equal to nothing works too. oApp = Nothing.

    Finally you can call a garbage collection with GC.Collect().
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: setting objects to nothing

    This is how I personally handle connection objects

    VB Code:
    1. Private Sub QueryDB()
    2. [INDENT]
    3. Dim oConn as SqlConnection
    4.  
    5. Try
    6.  
    7.     oConn = new SqlConnection("connection string")
    8.     oConn.Open()
    9.    ' execute query
    10.  
    11. Catch ex as Exception
    12.  
    13. Finally
    14.    If Not (oConn is Nothing) Then
    15.       With oConn
    16.          If .State <> StateClosed Then
    17.             .Close()
    18.          End If
    19.       End With
    20.        oConn = Nothing
    21.      
    22. End If
    23.  
    24. End Try
    25. [/INDENT]
    26.  
    27. End Sub
    Last edited by Mr.No; Mar 19th, 2006 at 05:26 AM.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: setting objects to nothing

    You don't set objects to Nothing. An object is something, not Nothing. You can set a variable to Nothing, so instead of referring to an object, which is something, it will refer to Nothing:
    VB Code:
    1. myVar = Nothing
    Note that this does not affect the object though. If an object has a Dispose method then you should call it when you have finished with the object. Setting a variable to Nothing generally has no useful effect, although there are some cases where it does. If your variable will lose scope immediately or almost immediately anyway then it is pointless.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: setting objects to nothing

    Yes, but you know we have this discussion allot but an Object can be set to Nothing. It just reinforces the Disposal of the object if it hasnt gone out of scope

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: setting objects to nothing

    Also if you have a load o references all pointing to the same object then setting one of those references to nothing will not affect either the object or the other references.
    I don't live here any more.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: setting objects to nothing

    Quote Originally Posted by Mr.No
    This is how I personally handle connection objects

    VB Code:
    1. Private Sub QueryDB()
    2. [INDENT]
    3. Dim oConn as SqlConnection
    4.  
    5. Try
    6.  
    7.     oConn = new SqlConnection("connection string")
    8.     oConn.Open()
    9.    ' execute query
    10.  
    11. Catch ex as Exception
    12.  
    13. Finally
    14.    If Not (oConn is Nothing) Then
    15.       With oConn
    16.          If .State <> StateClosed Then
    17.             .Close()
    18.          End If
    19.       End With
    20.        oConn = Nothing
    21.      
    22. End If
    23.  
    24. End Try
    25. [/INDENT]
    26.  
    27. End Sub
    You never actually dispose in that cleanup...


    VB Code:
    1. Finally
    2.    If Not (oConn is Nothing) Then
    3.       With oConn
    4.          If .State <> StateClosed Then
    5.             .Close()
    6.          End If
    7.          .Dispose
    8.       End With
    9.       oConn = Nothing
    10.     End If

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