0

I have an object X like this:

 X x1 = new X();

Here x1 has 3 properties say:

x1.a;
x1.b;
x1.c;

Here a, b and c are objects of other class.
Irrespective of these all I want to set the whole object x1 to null or set each property:

x1.a = null;
x1.b = null;

Is there any solution to accomplish this?

Meryovi
  • 6,121
  • 5
  • 42
  • 65
Anjana
  • 1,447
  • 5
  • 23
  • 33
  • 1
    Can I ask why? If its for gc then you don't need to worry – Sayse Aug 17 '13 at 08:39
  • You must set each of, because an object is null means that there is no any property, it may be object isn't null, but its properties are null – Elvin Mammadov Aug 17 '13 at 08:41
  • What do you want exactly?if you set x1=null; all of the properties haven't reference and they are not accessible. – Saman Gholami Aug 17 '13 at 08:42
  • @Sayse I wanted to display the values of the objects to NULL. Because of that i am in need of this.This has nothing to do with memory management. Thanks – Anjana Aug 17 '13 at 08:43
  • You do not have to explicitly set the objects/properties to null, unless they are external/unmanaged objects. Typically the GC will take care of this for you. – Adriaan Stander Aug 17 '13 at 08:43
  • 1
    NULL doesn't have a value? I don't understand what you are trying to acheive, this is an xy question – Sayse Aug 17 '13 at 08:44
  • @astander This question has no priority for GC.Thanks – Anjana Aug 17 '13 at 08:44
  • @Sayse . See my result ie Object x1 is returned to a WCF client where i needed to display as null.(This scenario is based on some conditions in the project). I think am able to explain the need. Thanks – Anjana Aug 17 '13 at 08:45
  • @Anjana what in the woulr would you mean when you say "This question has no priority for GC", Clearly the implications of which kind of object it is, and if the GC can manage it for you does make sense. Anyway, good luck on your way. – Adriaan Stander Aug 17 '13 at 08:46
  • 2
    Just display null then? you don't even need to know about the object, by all means check if it isn't null first and if it isn't then display null, but your question makes no sense – Sayse Aug 17 '13 at 08:47
  • @Sayse if the request is passing some value then i need to send response according to that. If the request is NULL then i need to set the response objects null + display a valis error message like"Request is null" This is what i needed.I think the question makes sense. Thanks – Anjana Aug 17 '13 at 08:50
  • It sounds like what you need is method scope and a messagebox – Sayse Aug 17 '13 at 08:52
  • @astander what i meant by "This question has no priority for GC", is including the discussion about GC and Dispose methods will make thsi discussion get deviated from the main stream. Thanks – Anjana Aug 17 '13 at 08:53

2 Answers2

4

Refer to the Tell-Dont-Ask principle. Don't ask object "X" about it's properties and manage them for the object. Instead, create a method so object X can manage itself:

class X
{
//...
  public void SetAllToNull()
  {
    this.a = null;
    this.b = null; 
    this.c = null;    
  }
}

X x = new X();
//...
x.SetAllToNull();

Why would you need this is another question. You may want to rethink your approach, carefully designing responsibilities of all objects.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130