I am using the type-safe enum pattern outlined here. I have a need to nest one type-safe enum into another. The child property(static object) is NULL at the time the parent constructor is created. It seems like the child constructor is not called and I'm getting some errors.(parent and child my be confusing, but it explains the hierarchy)
Here's an example(I'm using netMF):
public class MyDeviceSetting //parent
{
public readonly string Name;
public MyUnit SettingUnit;
public readonly MyUnit.UnitPurpose UnitPurpose;
#region MY STATIC SETTINGS
//UNIT SETTINGS
public static MyDeviceSetting TempUnits = new MyDeviceSetting("TempUnits", MyUnit.mm); //MyUnit.mm is null. Why?
public static MyDeviceSetting BLAH = new MyDeviceSetting("BLAH", MyUnit.inch);//MyUnit.inch is null. Why?
#endregion
/// <summary>
/// This is the MAIN PRIVATE Constructor
/// </summary>
/// <param name="?"></param>
private MyDeviceSetting(string name, MyUnit defaultUnit)
{
Name = name;
SettingUnit = defaultUnit;//NULL
UnitPurpose = SettingUnit.Purpose; //fails because SettingUnit is NULL
}
}
public sealed class MyUnit
{
private static int Count = 0;
//these are used to store and identify the unit in memory
public readonly int UnitID;
public readonly int TestID;
public enum UnitPurpose
{
DISTANCE,
SPEED,
TEMPERATURE,
TIME,
CLOCK,
NO_UNITS
}
public readonly string DisplayName;
public readonly string Abbreviation;
public readonly string Name;
public readonly UnitPurpose Purpose;
#region My Units
public static readonly MyUnit mm = new MyUnit("Milimeters", "mm", "mm", UnitPurpose.DISTANCE, 1);
public static readonly MyUnit inch = new MyUnit("inch", "inch", "in", UnitPurpose.DISTANCE, 2);
#endregion
private MyUnit(string name,
string displayName,
string abbreviation,
UnitPurpose unitPurpose,
int unitID)
{
Name = name;
DisplayName = displayName;
Abbreviation = abbreviation;
Purpose = unitPurpose;
UnitID = unitID;
TestID = Count;
Count++;
}
}
How do I ensure that child
is NOT null? Is there a work-around? Edit: This Post ensures that this should just work, but in my case it's not working.