I have 46 rows of information, 2 columns each row (“Code Number”, “Description”). These codes are returned to the client dependent upon the success or failure of their initial submission request. I do not want to use a database file (csv, sqlite, etc) for the storage/access. The closest type that I can think of for how I want these codes to be shown to the client is the exception class. Correct me if I’m wrong, but from what I can tell enums do not allow strings, though this sort of structure seemed the better option initially based on how it works (e.g. 100 = “missing name in request”).
Thinking about it, creating a class might be the best modus operandi. However I would appreciate more experienced advice or direction and input from those who might have been in a similar situation.
Currently this is what I have:
class ReturnCode
{
private int _code;
private string _message;
public ReturnCode(int code)
{
Code = code;
}
public int Code
{
get
{
return _code;
}
set
{
_code = value;
_message = RetrieveMessage(value);
}
}
public string Message { get { return _message; } }
private string RetrieveMessage(int value)
{
string message;
switch (value)
{
case 100:
message = "Request completed successfuly";
break;
case 201:
message = "Missing name in request.";
break;
default:
message = "Unexpected failure, please email for support";
break;
}
return message;
}
}
The best would be both a class and an enumeration. Then you can have more descriptive identifiers than “201”.
A structure would also work, but they are harder to implement correctly, so you should stick to a class unless you specifically need a structure for some reason.
You don’t need to store a reference to the message in the class, you can get that when needed in the
Messageproperty. Aswitchis implemented using a hash table (if there are five values or more), so the lookup is very fast.Usage:
If you get an integer code from somewhere, you can still use it as the enumerator values correspond to the codes:
(If the integer code doesn’t correspond to any of the identifiers in the enumeration, it’s still perfectly valid to do the conversion. When getting the
Messagevalue, it will end up in thedefaultcase as the value doesn’t match any of the other cases.)