zozig
asked on
Java Enum Question
Hi Everyone,
I'm trying to utilize an enum to dynamically generate error messages based an error code which is recieved. Here is an example of what I trying to do:
enum errorMsgs{
error_code1(1),
error_code(2);
private long errorCode
errorMsgs(long errorCode)
{
this.errorCode = errorCode;
}
pulic String getErrorMsg(long errorCode)
{
switch(this)
case error_code1: return "Message for error code 1";
case error_code2: return "Message for error code 2";
default: return "default error";
}
}
enum variable defined in class
errorMsgs _errors;
Elsewhere in the class:
long errorcode = connect.getErrorCode();
// Here is the question, how can I instantiate the enum class to dynamically select the errorcode such
// that the following is dynamic:
_error.error_code1;
// This method always returns the error code set above, which in this example is basically hard
// coded to error_code1, I need it to retrieve the message retrieved from connect.getErrorCode();
String errorMessage = _error.getErrorMessages(er rorcode);
Doesa anyone have any ideas how I can do this with enums? I would use the switch statement on a long but this is the JDK 1.5 and it doesn't allow switch statemenst on longs only on enums. I thought it would be good to explore the enum path as I've never used them before. Let me know if I need to clarify anything. Thanks.
I'm trying to utilize an enum to dynamically generate error messages based an error code which is recieved. Here is an example of what I trying to do:
enum errorMsgs{
error_code1(1),
error_code(2);
private long errorCode
errorMsgs(long errorCode)
{
this.errorCode = errorCode;
}
pulic String getErrorMsg(long errorCode)
{
switch(this)
case error_code1: return "Message for error code 1";
case error_code2: return "Message for error code 2";
default: return "default error";
}
}
enum variable defined in class
errorMsgs _errors;
Elsewhere in the class:
long errorcode = connect.getErrorCode();
// Here is the question, how can I instantiate the enum class to dynamically select the errorcode such
// that the following is dynamic:
_error.error_code1;
// This method always returns the error code set above, which in this example is basically hard
// coded to error_code1, I need it to retrieve the message retrieved from connect.getErrorCode();
String errorMessage = _error.getErrorMessages(er
Doesa anyone have any ideas how I can do this with enums? I would use the switch statement on a long but this is the JDK 1.5 and it doesn't allow switch statemenst on longs only on enums. I thought it would be good to explore the enum path as I've never used them before. Let me know if I need to clarify anything. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, typo
change this
pulic errorMsgs getErrorMsg(long errorCode)
to
pulic errorMsgs get(long errorCode)
change this
pulic errorMsgs getErrorMsg(long errorCode)
to
pulic errorMsgs get(long errorCode)
ASKER
Thanks for the response, however when I call get on the following code:
errorMsgs _errors = errorMsgs.get(1);
It cannot find the get method. I implemented the getErrorMsg method in my enum class but it stil can't find the get method. Is that a standard method for enums?