Null out the current object... possible?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the need to destory the current object when a method is called. Such
as:

public class Animal{
public string animalType = string.Empty;
public void Delete(){
this = null;
}
}

When I compile it, it throws an error. Is this not possible? I know the
user of class Animal can set it to null on his own, but was hoping I could do
this.

-AC
 
The object can't destroy itself. Its like if a person tried to eat his or
herself (don't get nasty now) it will come down to the point where the mouth
will have to eat the mouth and that is not possible!



If you need to destroy it, why not have the object that calls the "Delete"
method of the class also set the object to null?
 
When you say you need to destroy it, what do you actually need to do (I'm asking this because you can't do what you are suggesting in .NET)?

1. Release the resources used by the Animal?
2. Free up the memory consumed by the animal?
3. Remove the Animal from a collection?
4. Re-initialize the Animal back to its original state?
5. Make the Animal's consumer refer to a null reference rather than this Animal instance?
6. Something else?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I have the need to destory the current object when a method is called. Such
as:

public class Animal{
public string animalType = string.Empty;
public void Delete(){
this = null;
}
}

When I compile it, it throws an error. Is this not possible? I know the
user of class Animal can set it to null on his own, but was hoping I could do
this.

-AC
 
My specific case is my object is a CustomProperty. When the property is
Deleted, it removes it's record form the database and I wanted to destroy the
object at the same time.
 
Hi,

What you can do is post a message to some window (eg the main form, or a own
created hidden window), and in your custom message handler there you destroy
the object. That's how I should do it, but I'm just starting with NET so
maybe there are better ways.

rgds, Wilfried
 
Back
Top