|
-
Feb 16th, 2006, 11:23 AM
#1
Thread Starter
Frenzied Member
setting objects to nothing
I have the following code.
VB Code:
Dim obj as SomeDataObject = New SomeDataObject(1) '1 is the id in the database
obj.delete 'will delete the object from the table.
How can I make it so that if delete is called on that object,it will set itself to nothing? I don't want that object to be valid anymore if the Delete method is called.
Don't anthropomorphize computers -- they hate it
-
Feb 16th, 2006, 11:48 AM
#2
Re: setting objects to nothing
well you can just say
But its only of any use if the object your using is global. The object will automatically be set to nothing when out of scope (end of sub or dispose of a class). Some members can be disposed manually, usually obj.dispose but using .Net its hardly worth doing. Its clever enough to know when to keep and when to release an objects memory.
-
Feb 16th, 2006, 11:50 AM
#3
Re: setting objects to nothing
Is this a custom class you wrote? an object can not set its self to nothing, as inside a class, the class can not be the target of an assignment (me can't be on the left side of an = sign)
what you could do is have your class implement IDisposable, which adds a dispose method to your class, which lets users of the class know that it should be called when the object should be destroyed. However it is up the the user of the class to call the dispose method when the class is doing being used.
-
Feb 16th, 2006, 11:53 AM
#4
Re: setting objects to nothing
You can have an object Dispose itself to release resources but you can't have it set the variable to Nothing. You have to do that yourself. Remember, setting a variable to Nothing doesn't affect the object at all. It merely means that that variable doesn't refer to that object anymore. It's quite possible that other variables will still refer to the object though.
-
Feb 16th, 2006, 12:01 PM
#5
Re: setting objects to nothing
 Originally Posted by Grimfort
usually obj.dispose but using .Net its hardly worth doing. Its clever enough to know when to keep and when to release an objects memory.
That's a common misconception. .NET does a lot for you as far as memory management is concerned but you still have to do your share. If you do not Dispose objects that require it then they may retain valuable resources until the garbage collector gets around to reclaiming their memory, which may be some time. Even if your class doesn't hold system resources itself, it may have member variables that do. Any time an instance of your class has member variables that have a Dispose method, your class should have a Dispose method too, to at least call the Dispose methods of its members.
-
Feb 16th, 2006, 12:33 PM
#6
Re: setting objects to nothing
 Originally Posted by jmcilhinney
That's a common misconception. .NET does a lot for you as far as memory management is concerned but you still have to do your share. If you do not Dispose objects that require it then they may retain valuable resources until the garbage collector gets around to reclaiming their memory, which may be some time. Even if your class doesn't hold system resources itself, it may have member variables that do. Any time an instance of your class has member variables that have a Dispose method, your class should have a Dispose method too, to at least call the Dispose methods of its members.
Exactly... Grimfort, This is why some objects (like an xmldocument) don't have a dispose method, and other objects (like a SQLConnection) do. MS has given dispose methods in the framework classes where it was needed to do so, and left it out of others where it was not needed. People writing their own classes need to do the same.
-
Feb 16th, 2006, 12:44 PM
#7
Thread Starter
Frenzied Member
Re: setting objects to nothing
Yeah, It doesn implement idisposable. But, calling the disposable method doesn't destroy the object. It just releases any references the object has. I still have to wait for the GC to kill it.
Thanks,
Don't anthropomorphize computers -- they hate it
-
Feb 16th, 2006, 03:29 PM
#8
Re: setting objects to nothing
Aye, I know all that, I was attempting at a simple scenario to get the poster onto the idea of it rather then each and every case where you may have to do it yourself. Why write every possible scenario. Its the old, too much information over simple not enough info argument .
-
Feb 16th, 2006, 05:22 PM
#9
Re: setting objects to nothing
 Originally Posted by vbgladiator
Yeah, It doesn implement idisposable. But, calling the disposable method doesn't destroy the object. It just releases any references the object has. I still have to wait for the GC to kill it.
Thanks,
Yeah, that's the general pattern. It sounds like this is not sufficient for you. Is this because of memory issues?
You can invoke the GC directly, to hasten the recovery, but I'm wondering what the underlying issue is that makes you want to destroy the object so quickly.
My usual boring signature: Nothing
 
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
|