using enum or old-fashion way in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    using enum or old-fashion way in C#

    I am porting old java code to csharp and now facing a stumbling block.
    Before advent of enum in Java, developers used enum-like structures,
    shown below. However, AFAIK, CSharp isn't lacking this particular
    feature, and I don't want to port it in an old way. Here is the code
    below (in java ) that I want to approximate in C#. Note that I have
    lots of classes that follow this sort of principles.


    public class Derived extends Base {


    public static final int FOO_VAL = 0 ;
    public static final int BAR_VAL = 1 ;
    ;


    public static final Derived FOO = new Derived(FOO_VAL , "FOO");

    public static final Derived BAR = new Derived(BAR_VAL , "BAR");

    private Derived(int aValue) {

    super(aValue);
    }

    private Derived(int aValue, String aString) {

    super(aValue, aString);
    }

    public static Derived getType (int aValue) {

    if ( aValue == FOO_VAL ) return FOO;
    if ( aValue == BAR_VAL ) return BAR;

    return NEW;
    }
    //Getting type accross the wire
    public static OrderType getType (
    final InputStream is )
    throws IOException, IllegalReadExce ption {

    final int[] val = new int[1];
    is.getNextField (val);
    return getType(val[0]);
    }

    public static OrderType getType (String aValue)
    {
    if ( aValue.equals(F oo.toString()) ) return BAR;
    if ( aValue.equals(B ar.toString()) ) return FOO;
    return BAR;
    }
    }

    -----
    Thanks,

    puzzlecracker
  • puzzlecracker

    #2
    Re: using enum or old-fashion way in C#

    am porting old java code to csharp and now facing a stumbling block.
    Before advent of enum in Java, developers used enum-like structures,
    shown below. However, AFAIK, CSharp isn't lacking this particular
    feature, and I don't want to port it in an old way. Here is the code
    below (in java ) that I want to approximate in C#. Note that I have
    lots of classes that follow this sort of principles.

    public class Derived extends Base {

    public static final int FOO_VAL = 0 ;
    public static final int BAR_VAL = 1 ;
    ;

    public static final Derived FOO = new Derived(FOO_VAL , "FOO");

    public static final Derived BAR = new Derived(BAR_VAL , "BAR");

    private Derived(int aValue) {

    super(aValue);
    }

    private Derived(int aValue, String aString) {

    super(aValue, aString);
    }

    public static Derived getType (int aValue) {

    if ( aValue == FOO_VAL ) return FOO;
    if ( aValue == BAR_VAL ) return BAR;

    return NEW;
    }
    //Getting type accross the wire
    public static Derived getType (
    final InputStream is )
    throws IOException, IllegalReadExce ption {

    final int[] val = new int[1];
    is.getNextField (val);
    return getType(val[0]);
    }

    public static Derived getType (String aValue)
    {
    if ( aValue.equals(F oo.toString()) ) return BAR;
    if ( aValue.equals(B ar.toString()) ) return FOO;
    return BAR;
    }

    }

    -----
    Thanks,

    puzzlecracker

    Comment

    • Peter Duniho

      #3
      Re: using enum or old-fashion way in C#

      On Sun, 28 Sep 2008 15:45:04 -0700, puzzlecracker <ironsel2000@gm ail.com>
      wrote:
      I am porting old java code to csharp and now facing a stumbling block.
      Before advent of enum in Java, developers used enum-like structures,
      shown below. However, AFAIK, CSharp isn't lacking this particular
      feature, and I don't want to port it in an old way. Here is the code
      below (in java ) that I want to approximate in C#. Note that I have
      lots of classes that follow this sort of principles.
      Enums in C# are value types and can't be inherited. So you can't combine
      non-enum functionality with enum functionality, as shown in the code
      sample you posted.

      If you're willing to separate the two aspects into different types then
      yes, you could probably replace the enum-like aspect with an actual C#
      enum. Converting between strings and enums using the Enum.Parse() method
      is straight-forward, as is casting the base type (int by default) to and
      from an enum.

      Other than that, without knowing what's in the class "Base", it's hard to
      even fully comprehend your sample, never mind infer a question from it.
      Was there in fact a specific question you wanted an answer for?

      Pete

      Comment

      Working...