-4

I need to write this code in C#. Any help would be appreciated. The interface and the first class is where the issues are. How can I convert that into C#? EnumByteConverter & EnumByteConverter>? Is this even possible? What would my options be?

public interface EnumByteConverter<E extends Enum<E> & EnumByteConverter<E>>
{
  byte convert();
  E convert(byte val);
}




public class ReverseByteEnumMap<V extends Enum<V> & EnumByteConverter>
{
  private Map<Byte, V> map = new HashMap<>();

  public ReverseByteEnumMap(Class<V> valueType)
  {
    for (V v : valueType.getEnumConstants())
    {
      map.put(v.convert(), v);
    }
  }

  public V get(byte num)
  {
    return map.get(num);
  }
}




public enum Transport implements EnumByteConverter<Transport>
{
  FrameFormatB(0), 
  FrameFormatA(1), 
  FrameDraft(254),
  FrameUnknown(255);

  private static ReverseByteEnumMap<Transport> map = new ReverseByteEnumMap<>(Transport.class);
  private final byte value;

  Transport(int value)
  {
    this.value = (byte) value;
  }

  public byte convert()
  {
    return value;
  }

  public Transport convert(byte val)
  {
    return map.get(val);
  }

  public static Transport get(byte val)
  {
    return map.get(val);
  }
}

This is what I have tried. I managed to implement the interface using struct, and the reverse class with similar thing.

  public interface IEnumByteConverter<E> where E : struct
  {
    byte Convert();
    E Convert(byte val);
  }
}



  public class ReverseByteEnumMap<T> where T : struct, IEnumByteConverter<T>
  {
    private Dictionary<Byte, T> map = new Dictionary<Byte, T>();
    private IEnumByteConverter<T> converter;

    public ReverseByteEnumMap(T valueType)
    {
      foreach (T t in Enum.GetValues(typeof(T)))
        map.Add(t.Convert() , t);
    }

    public T Get(byte num)
    {
      return map[num];
    }
  }


  public class Transport : IEnumByteConverter<TransportEnums>
  {

    public enum TransportEnums
    {
      FrameFormatB = 0,
      FrameFormatA = 1,
      FrameDraft = 254,
      FrameUnknown = 255
    }

    private static ReverseByteEnumMap<Transport> map = new ReverseByteEnumMap<Transport> ();
    private byte value;

    Transport(int value)
    {
      this.value = (byte)value;
    }

    public byte Convert()
    {
      return value;
    }

    public TransportEnums Convert(byte val)
    {
      return map.Get(val);
    }

    public static TransportEnums Get(byte val)
    {
      return map.Get(val);
    }

  }

But I got stuck in the last class. I cannot use the Transport.class nor enums. I cannot tell if this a right path I am heading on

Calliste
  • 23
  • 2
  • What did you try? – Lucas Jun 06 '18 at 09:46
  • 3
    It's perfectly possible but unfortunately SO isn't a code writing service. Feel free to come back with a specific on-topic question! – Adriano Repetti Jun 06 '18 at 09:46
  • @Lucas In his defense, the code seems to be a little complex. I wouldn't know how to translate it (but I don't do java). It would be more easy for me to rework it based on the specifications than by translating it line-by-line. – xanatos Jun 06 '18 at 09:47
  • 1
    @AdrianoRepetti I'll say that translating 1:1 is absolutely impossible. In C# you can't subclass an `enum`. You can try to build something similar, but it won't be very much similar. – xanatos Jun 06 '18 at 09:48
  • @calliste As is the code can't be translated. What is possible is, given enough use cases to build something similar. – xanatos Jun 06 '18 at 09:50
  • I wasn't asking for a service, but I trust stackoverflow community to help each other, and that is what I was looking for: a little guidance. – Calliste Jun 06 '18 at 09:52
  • It would be better if you showed us what you tried to do and ask for help – Clive Makamara Jun 06 '18 at 09:53
  • I have updated the question and how I tried to implement all this – Calliste Jun 06 '18 at 10:26
  • @xanatos obviously 1:1 translation is impossible unless you're working with a superset/subset, no one asked for that. point is that you _can_ translate from Java to C# the same way you translate from German to French. – Adriano Repetti Jun 06 '18 at 11:15

1 Answers1

1

In C# it is (quite) easy to change the base type of an enum:

public enum Transport : byte
{
    FrameFormatB = 0,
    FrameFormatA = 1,
    FrameDraft = 254,
    FrameUnknown = 255
}

and it is quite easy to convert from the enum to the base type and back:

Transport t1 = (Transport)255; // Equivalent to = Transport.FrameUnknown

int i1 = (int)t1; // 255
byte b1 = (byte)t1; // 255

So you don't need all the support code that you wrote.

In Java enum are classes that don't normally have a "value", and if you want to give them a numerical value you have to do like what is written in your code: define a value field. Then you have to add methods to go from the enum "name" to its "value" and back.

The main problem here is that the C# (.NET) enum can in truth have any value, not only the defined ones:

Transport t2 = (Transport)100;

while in Java only the defined values of enum are legal (because they are the only instances of the enum class that are defined. Other instances can't be created). See for example https://stackoverflow.com/a/469315/613130 .

The code you wrote is quite similar to this one: http://dan.clarke.name/2011/07/enum-in-java-with-int-conversion/

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Thank you for the answer @xanatos I will check the links. It is only how the interface was written. And when it is not your code, it is harder to work your way around it – Calliste Jun 06 '18 at 10:25