Static objects are null / not initialized at the correct time

I use the type of safe enum type described here. I need to enclose one secure key in another. The child property (static object) is NULL when the parent constructor is created. It seems that the child constructor is not being called, and I am getting some errors. (Parent and child confuse me, but he explains the hierarchy)

Here is an example (I use 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 can I guarantee that childit is not null? Is there any work? Edit: This post guarantees that this should work, but in my case it does not work. Null

+4
4

/ .Net Micro framework. . -, : https://msdn.microsoft.com/en-us/library/Cc533032.aspx

NetCF 3.0 :

. .NET Micro Framework.

, ( , 2.0) :

, .NET Compact Framework , .NET Framework. , , .NET Compact Framework V2

, , , .

, , , . - :

private static MyUnit inch;

public static MyUnit Inch
{
    get
    {
        if (inch == null)
            inch = new MyUnit("inch", "inch", "in", UnitPurpose.DISTANCE, 2);
         return inch;
    }
}

, , , . , , , , .

+1

MyUnit MyUnit.cs, , CLR CLR .

, .Netmf 4.3 RTM. enter image description here

+1

, , .

MyUnit, MyDeviceSetting , MyUnit .

, .

public class MyDeviceSetting //parent
{
    static MyDeviceSetting()
    {
        var dummy = MyUnit.inch;
    }
}

, dummy var , MyUnit .

MyDeviceSetting MyUnit .

0

@shf301 , . , /- .

Renaming the child class on MyAUnitmakes it accessible before MyDeviceSettingand seems to run static members MyAUnitbefore MyDeviceSettingstatic memebers.

It seems to work, but for now it’s a hack.

0
source

All Articles