Help tracking class property initializations

D

dvestal

I have a class with public members, and I need to know whether those
members have been explicitly initialized. I could accomplish that
with a class like this:

class C
{
private bool m_isSet = false;
private object m_object;

public object MyObj
{
get
{
return m_object;
}
set
{
m_object = value;
m_isSet = true;
}
}
}

....but for my purposes, that's way too verbose. I need a way to
achieve that functionality, but write the class much more concisely.
Does anybody know a way to do this while allowing a shorter class
definition, such as the one below?

class C
{
// Is there any way to know when
// MyObj is set?
public object MyObj;
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

...but for my purposes, that's way too verbose. I need a way to
achieve that functionality, but write the class much more concisely.
Does anybody know a way to do this while allowing a shorter class
definition, such as the one below?

class C
{
// Is there any way to know when
// MyObj is set?
public object MyObj;
}

In general there is no way of doing it, if you only have references types
you could check if they are not null. This fails for valued types, as they
always have a value. Additionally the method will also fails with references
types that the class initialize in the constructor.

You will have to use the verbose method I think
 
N

Nicholas Paldino [.NET/C# MVP]

That only applies if null is not a valid state for the reference type
fields that he has. If you can explicitly set the value to null, then you
still need the flag.

On the flip side, as of .NET 2.0, you could use a nullable type for the
backing field (assuming null is not allowed as a value in the range of
values) for value types, and then check for null to see if the value has
been set (while the property exposed is not nullable).

You could get away with this by using a context bound object, and
intercepting the calls, but that is a lot of work, and there is going to be
a bit of overhead to intercept the calls. However, it would allow for a
more concise implementation (the interception layer for the context bound
object would handle whether or not a property was explicitly set).
 
D

dvestal

I want to use both value and reference types, and null may be a valid
value. I will check into context-bound objects (haven't heard of them
before).

In the meantime, I can get a reasonably concise class definition using
a generic MessageProperty class, as shown below. Implicit conversion
operators allow me to use the C.MyObj member as whatever type I
choose. I don't see a significant downside to this approach, but I
welcome comment.

public class MessageProperty<T>
{
private T m_value;
private bool m_isSet = false;

public T Value
{
get
{
return m_value;
}
set
{
m_value = value;
m_isSet = true;
}
}

public bool IsSet
{
get { return m_isSet; }
}

static public implicit operator T(MessageProperty<T> property)
{
return property.Value;
}

static public implicit operator MessageProperty<T>(T value)
{
MessageProperty<T> prop = new MessageProperty<T>();
prop.Value = value;
return prop;
}
}

public class C
{
public MessageProperty<object> MyObj = new
MessageProperty<object>();
}
 
N

Nicholas Paldino [.NET/C# MVP]

I actually think that's a pretty elegant way to handle it. It's much
better than context bound objects, as you won't have the overhead of
interception. I'd definitely use this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top