I have a function which return a type int. However, I only have a value of the TAX enumeration.
How can I cast the TAX enumeration value to an int?
JavaScript
x
public enum TAX {
NOTAX(0),SALESTAX(10),IMPORTEDTAX(5);
private int value;
private TAX(int value){
this.value = value;
}
}
TAX var = TAX.NOTAX; // This value will differ
public int getTaxValue()
{
// what do do here?
// return (int)var;
}
Advertisement
Answer
You’d need to make the enum expose value
somehow, e.g.
JavaScript
public enum Tax {
NONE(0), SALES(10), IMPORT(5);
private final int value;
private Tax(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public int getTaxValue() {
Tax tax = Tax.NONE; // Or whatever
return tax.getValue();
}
(I’ve changed the names to be a bit more conventional and readable, btw.)
This is assuming you want the value assigned in the constructor. If that’s not what you want, you’ll need to give us more information.