using enum or old-fashion way in C#

P

puzzlecracker

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, IllegalReadException {

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

public static OrderType getType (String aValue)
{
if ( aValue.equals(Foo.toString()) ) return BAR;
if ( aValue.equals(Bar.toString()) ) return FOO;
return BAR;
}
}
 
P

puzzlecracker

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, IllegalReadException {

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

public static Derived getType (String aValue)
{
if ( aValue.equals(Foo.toString()) ) return BAR;
if ( aValue.equals(Bar.toString()) ) return FOO;
return BAR;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top