I am making an "error code to String" converter that would display the name of an error code from its value, for example 0x000000c3 would give "Class not found", but using MY OWN error codes!
Here's how it actually looks like:
#region errcodes
public int NORMAL_STOP = 0x00000000;
public int LIB_BROKEN = 0x000000a1;
public int RESOURCE_MISSING = 0x000000a2;
public int METHOD_NOT_FOUND = 0x000000a3;
public int FRAMEWORK_ERROR = 0x000000b1;
public int UNKNOWN = 0x000000ff;
#endregion
public string getName(int CODE)
{
}
I would like to get a string value from parameter CODE, in function getName.
How can I do that ?
A good C# practice is using of an enum:
public enum ErrorCode
{
NORMAL_STOP = 0x00000000,
LIB_BROKEN = 0x000000a1,
RESOURCE_MISSING = 0x000000a2,
METHOD_NOT_FOUND = 0x000000a3,
FRAMEWORK_ERROR = 0x000000b1,
UNKNOWN = 0x000000ff
}
public const string InvalidErrorCodeMessage = "Class not found";
public static string GetName(ErrorCode code)
{
var isExist = Enum.IsDefined(typeof(ErrorCode), code);
return isExist ? code.ToString() : InvalidErrorCodeMessage;
}
public static string GetName(int code)
{
return GetName((ErrorCode)code);
}
Another good advice: it would be great to use the C# naming convention for error codes:
public enum ErrorCode
{
NormalStop = 0x00000000,
LibBroken = 0x000000a1,
ResourceMissing = 0x000000a2,
MethodNotFound = 0x000000a3,
FrameworkError = 0x000000b1,
Unknown = 0x000000ff
}
Usage example:
void Main()
{
Console.WriteLine(GetName(0)); // NormalStop
Console.WriteLine(GetName(1)); // Class not found
Console.WriteLine(GetName(ErrorCode.Unknown)); // Unknown
}
User contributions licensed under CC BY-SA 3.0