Search code examples
c#listenumsconstantsuint

C# constant list of values


I want control a other device with a C# program, this device have something like registers for read and write. So I searching something to enter all register numbers to get a better human readable code and to have central config place. All the registers using a uint value.

I searching something like this:

public enum EISCJoin : uint
{
    Read_Connect = 1,
    Read_Temp = 2,
    Read_Switch = 3,
    Write_Connect = 1,
    Write_Temp = 2,
    Write_Switch = 3,
}

switch (args.Sig.Number) //uint value
{   
    case (uint)EISC.Read_Connect:
        {
            args.SendValue[(uint)Write_Connect] = 1;
            ...
            break;
        }
    case (uint)EISC.Read_Temp:
        {
            args.SendValue[(uint)Write_Temp] = 1;
            ...
            break;
        }
    case (uint)EISC.Read_Switch:
        {
            args.SendValue[(uint)Write_Switch] = 1;
            ...
            break;
        }
}

My problem is that I don't want cast the ENUM value thousands of times in my source code and what I know is that a implicit conversation is not possible with enum.

Have someone a good idea to create a constant list of uint values?


Solution

  • you need to cast the number instead:

    switch (args.Sig.Number)
    

    to

    switch ((EISCJoin)args.Sig.Number)
    

    The other way is to use a static class and constant uints:

    static public class EISCJoin
    {
        public const uint Read_Connect = 1;
        public const uint Read_Temp = 2;
        // and so on
    }