Troj
Troj

Reputation: 11911

.Net (C#) Enum rewritten to java

I have an Enum in .Net. How can I rewrite this Enum in java?

Here is the Enum:

public enum AdCategoryType : short
{
    ForSale = 1,
    ForBuy = 2,
    ForRent = 8,
    WantingForRent = 16,
    WorkIsWanted = 32, 
    WorkIsGiven = 64
}

Upvotes: 3

Views: 347

Answers (3)

duffymo
duffymo

Reputation: 308958

This gets you the enum:

public enum AdCategoryType {

    ForSale(1),

    ForBuy(2),

    ForRent(4),

    WantingForRent(8),

    WorkIsWanted(16),

    WorkIsGiven(32);

    private final int value;

    AdCategoryType(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }
}

Upvotes: 2

Margus
Margus

Reputation: 20058

This will work:

public enum AdCategoryType {
    ForSale/*           */ (1 << 0), //
    ForBuy/*            */ (1 << 1), //
    ForRent/*           */ (1 << 2), //
    WantingForRent/*    */ (1 << 3), //
    WorkIsWanted/*      */ (1 << 4), //
    WorkIsGiven/*       */ (1 << 5);
    private final int value;

    private AdCategoryType(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }
};

To get the value of ForBuy use AdCategoryType.ForBuy.getValue().

Upvotes: 2

Burleigh Bear
Burleigh Bear

Reputation: 3314

public enum Foo
{
   Bar(1),
   Baz(45);

   private short myValue;

   private short value()
   {
     return myValue;
   }

   public Foo(short val)
   {
      myValue = val;
   }

}

Upvotes: 0

Related Questions