Preskočiť na hlavný obsah

How to use Enum in switch statements

If you wana use enums in a switch, don't google, I did...

Enum switch case label must be the unqualified name of an enumeration constant.

So let's understand this with a simple example:

enum EnumType { INT,DATE, FLOAT}
EnumType someVar;
...

switch (someVar) {
case (EnumType.INT):
handle(EnumType.INT);
...
}

This looks fine, so? Unfortunately, it is wrong in two different ways.

The case exp should be without the brackets and only INT should be used without the EnumType type def. Let's illustrate within a simple correction:

switch (someVar) {
case INT:
handle(EnumType.INT);
...
}

Summarization:
* In case statement the enum must be used without brackets.
* In case only the unqualified enum name must be used.

Obľúbené príspevky z tohto blogu

mysql 5.0 upgrade to 5.1

The 5.1 series of MySQLwas unmasked for the gentoo portage. When upgrading from an older major version (including 5.0), you will be required to rebuild everything linked to the libmysqlclient.so.15 and libmysqlclient_r.so.15. You can do this by installing app-portage/gentoolkit and running: # revdep-rebuild --library libmysqlclient.so.15 # revdep-rebuild --library libmysqlclient_r.so.15 If you use the Portage 2.2 series, you may also use: # emerge @preserved-rebuild The official upgrade documentation is available here: http://dev.mysql.com/doc/refman/5.1/en/upgrading.html Note that existing databases may need converting as well, again including those upgrading from 5.0 to 5.1.