|
-
Mar 19th, 2006, 02:45 AM
#1
Thread Starter
Frenzied Member
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
-
Mar 19th, 2006, 04:37 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 19th, 2006, 05:01 AM
#3
Fanatic Member
Re: setting objects to nothing
This is how I personally handle connection objects
VB Code:
Private Sub QueryDB()
[INDENT]
Dim oConn as SqlConnection
Try
oConn = new SqlConnection("connection string")
oConn.Open()
' execute query
Catch ex as Exception
Finally
If Not (oConn is Nothing) Then
With oConn
If .State <> StateClosed Then
.Close()
End If
End With
oConn = Nothing
End If
End Try
[/INDENT]
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 ...
-
Mar 19th, 2006, 07:00 AM
#4
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: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.
-
Mar 19th, 2006, 07:03 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 19th, 2006, 09:55 AM
#6
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.
-
Mar 19th, 2006, 10:23 AM
#7
Re: setting objects to nothing
 Originally Posted by Mr.No
This is how I personally handle connection objects
VB Code:
Private Sub QueryDB()
[INDENT]
Dim oConn as SqlConnection
Try
oConn = new SqlConnection("connection string")
oConn.Open()
' execute query
Catch ex as Exception
Finally
If Not (oConn is Nothing) Then
With oConn
If .State <> StateClosed Then
.Close()
End If
End With
oConn = Nothing
End If
End Try
[/INDENT]
End Sub
You never actually dispose in that cleanup...
VB Code:
Finally
If Not (oConn is Nothing) Then
With oConn
If .State <> StateClosed Then
.Close()
End If
.Dispose
End With
oConn = Nothing
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|