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.
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.
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.
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.
Re: setting objects to nothing
Quote:
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.
Re: setting objects to nothing
Quote:
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.
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,
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 ;).
Re: setting objects to nothing
Quote:
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.