Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net (C#) Enum rewritten to java

Tags:

java

c#

.net

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
}
like image 679
Troj Avatar asked Jun 29 '26 08:06

Troj


2 Answers

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;
    }
}
like image 73
duffymo Avatar answered Jul 01 '26 21:07

duffymo


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().

like image 45
Margus Avatar answered Jul 01 '26 20:07

Margus