I am looking for some advice/suggestions on how best to implement some properties in my class. Consider the following 2 classes:
public class ClassA
{
// some contents for the class
}
// The following class contains an object of ClassA that
// may or may not be instantiated at all during the lifetime of ClassB
public class ClassB
{
private ClassA m_classa = null;
// Then I have a whole bunch of properties that return a value
// which is either a property of ClassA, if instantiated, or a default value
// For e.g.
public string PropertyOne
{
get
{
return m_classa != null ? m_classa.PropertyOne : string.Empty;
}
}
// The above pattern repeats for all of the properties that I have in ClassB
}
With this approach, I have to do the null check, and specify the default value for every property that I need to implement. It seems like there maybe a better way to do this, especially if I have a lot of properties.
My question is: is this the best way to do this? If not, what are some other approaches that I can take to minimize the amount of code I have to write, and improve readability?
Thanks!