0

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?

  • what if you write : ``case EISC.Read_Connect`` – Ehsan Sajjad Nov 17 '15 at 19:07
  • Can you make `args.Sig.Number` a `EISCJoin` type? – Steve Wellens Nov 17 '15 at 19:09
  • 1
    Just cast your `args.Sig.Number` value to `EISCJoin` instead. Note that currently `Read_Connect` and `Write_Connect` have the same value, mind you... – Jon Skeet Nov 17 '15 at 19:09
  • There might be a couple much simpler ways of doing it considering there are "thousands" but can you clarify, `args.SendValue[(uint)Write_Temp]` should that actually be `args.SendValue[(uint)EISCJoin.Write_Temp]`? Are you always sending `1` (I'm guessing `true`)? And what goes on in `...` is it custom logic for each different one, or is it generic? – amg-argh Nov 17 '15 at 19:32

2 Answers2

3

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
}
GreatAndPowerfulOz
  • 1,767
  • 13
  • 19
  • the switch is only a example, I need it on thousand of other positions in the code also. That was my fear, do it with const variables, because it is a lot more work. Thanks – DangerSchwob Nov 17 '15 at 19:11
  • @DangerSchwob, the other approach is what dsblinkenlight mentioned, and that is to change all your other structs/classes to use the `enum` instead of an `int` or a `uint` as the type of the field or property. – GreatAndPowerfulOz Nov 17 '15 at 19:22
0

I must work with a framework of the manufacturer and have no access to all the classes, it is not a option to modify all classes to the enum.

I using a value the most time only 1-2 times, so the most time I cast the value for 1 time use.

It looks like I must use the static class with the const uint variables.

Thanks for your help