My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://kavachai.com
and update your bookmarks.

Wednesday, June 17, 2009

There is no "enum" keyword for BlackBerry

In one of projects I'm working on I needed to port some code from Android to BlackBerry platform. As part of implementation for Android I used enumeration.
As I saw there is no "enum" keyword for BlackBerry. That was hard to understand since I like enums as a natural way to group constants. Anyway I had two options: use simple constants or design a class. I decided on the second one.

So for enum that looks like this:
enum Numers {
Odd, Twin;

public int toInt() {
return (this == Odd) ? 0 : 1;
}

public static Numbers fromInt(int value) {
return (value == 0) ? Odd : Twin;
}
}
I used class like this:
class Numbers {
public static Numbers Odd = new Numbers(0);
public static Numbers Twin = new Numbers(1);

private int _value;

private Numbers(int value) {
_value = value;
}

public int toInt() {
return this._value;
}

public static Numbers fromInt(int value) {
return (value == 0) ? Odd : Twin;
}
}
Not so natural as with "enum" keyword, but good enough :)

No comments:

Post a Comment