0

Lets have a simple class with an int property

    public class SimpleClass {
      public int myInt { get; set; }// for having a property and not "public int myInt;", see Jon Skeet remark
    }

I instanciate it twice assigning myInt or not

    assignedObject = new SimpleClass() { myInt=0};
    notAssignedObject = new SimpleClass();

Now by reflection, I query the value of myInt in each case using

    Object value;
    value=assignedObject.GetType().GetProperties().Where(o=>o.Name.Equals("myInt")).First().GetValue(assignedObject,null)
    value=notAssignedObject.GetType().GetProperties().Where(o=>o.Name.Equals("myInt")).First().GetValue(notAssignedObject,null)

I am getting twice 0 for myInt, but I need to be able to differenciate them. How?

tit
  • 599
  • 3
  • 6
  • 25

1 Answers1

3

Unless you have code to specifically remember the difference between a property which has been initialized with its default value, and one which hasn't been set at all, you can't tell the difference. I have two suggestions, however:

  • You could make it an int? property, and then check whether the value is null. Of course, it's possible for it to be set to null explicitly, unless you prohibit that, but it may be good enough.
  • You could keep a separate bool value to know whether or not it's been set explicitly, and just set it in your property setter, along with the value.

Sample code for the second option:

public class SimpleClass
{
    private int value;
    private bool valueSet;

    public int Value
    {
        get { return value; }
        set
        {
            this.value = value;
            this.valueSet = true;
        }
    }

    public bool ValueSet { get { return valueSet; } }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have corrected SimpleClass and put myInt as a property.Thanks. Using int? seems not adapted since SimpleClass targets a database model. Need to think about putting it explicitely to null but I just realize that I use such properties as fields. The second possibility is a bit overhead since I target many properties. – tit Oct 05 '14 at 16:12
  • @thipages: I'm not sure what you mean by "I use such properties as fields". To be honest, it's not really clear what your requirements are - and they certainly weren't clear from the question - but *in general* there's no difference between a property which has been set to the default value for that type, and one which hasn't been set at all. – Jon Skeet Oct 05 '14 at 16:17
  • "I use such properties as fields", I meant that I declare public myInt{get;set;} and use it in my code as it were declared as public int myInt; "but in general there's no difference..." answers my question. – tit Oct 05 '14 at 16:25
  • @thipages: Right - you mean "I usually just declare automatically implemented properties." (That's what that syntax is called.) It's not the same as declaring a public field though, in various ways. (For example, you can pass a field by reference, but not a property.) – Jon Skeet Oct 05 '14 at 16:28