Tips for creating an error code in C #

I'm new to C #, and so forgive me if I come up with some of my concepts. I work with Canon EDSDK, and to make life easier, I would like to see error messages as text, not hexadecimal. The EDSDK.cs file contains a long list of errors, such as:

public const uint EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07;
public const uint EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08;

Ideally, I would like to put all this in some sort of lookup table so that I can enter the HEX error code and return the actual message as a string. for instance

Hashtable EDSDKErrorCodes = new Hashtable();

EDSDKErrorCodes.Add("0x00008D01", "EDS_ERR_TAKE_PICTURE_AF_NG");
EDSDKErrorCodes.Add("0x00008D08", "EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG");
etc etc...

Later in my code, I could receive the error message returned by one of the EDSDK methods and display it in a user-friendly form:

errorcode= EDSDK.EdsInitializeSDK();

MessageBox.Show(Convert.ToString(EDSDKErrorCodes[errorcode]));

... , ? , - , Add?

+3
6

.

public enum ErrorCodes : uint
{
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

:

ErrorCodes error = (ErrorCodes)EDSDK.EdsInitializeSDK();
MessageBox.Show(Enum.GetName(typeof(ErrorCodes), error));
+6

- Enum description. . . , :

public enum ErrorCodes : uint
{
    [Description("Description for EDS_ERR_TAKE_PICTURE_CARD_NG")]
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    [Description("Description for EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG")]
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

, , , Enum.GetName

+12

, :

public enum CanonErrorCode
{
    SomeErrorDescription = 14,
    SomeOtherErrorDescription = 15
    // .. etc
}

hex integer. :

// Samples
var myErrorCode = (CanonErrorCode)someIntegerValue;
var myErrorCode = CanonErrorCode.SomeOtherErrorDescription;

, , - , :

static Dictionary<int, string> errorlookups = new Dictionary<int, string>();

errorLookups.Add(10, "This is the human readable error message.");
errorLookups.Add(17, "Create this guy once in a static constructor.");
+3

-

Dictionary<uint, String> myDict = new Dictionary<uint, String>();

,

MessageBox.Show(myDict[errorCode]);

, , - , EDSDK uint. , .

+2

, , :-)
, .

Enum :

public enum ErrorCode : uint
{
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

:

public static class ErrorHandler
{
    public static string ToErrorString(this ErrorCode errorCode)
    {
        return Enum.IsDefined(typeof(ErrorCode), errorCode) ? 
            errorCode.ToString() : "Undefined error code";
    }
}

, , :

var errorCode = (ErrorCode)EDSDK.EdsInitializeSDK();
MessageBox.Show(errorCode.ToErrorString());
+1

, -. -, Canon EDSDK, , , uint, . , ? , , , .

, - :

Dictionary<int,String> EDSDKErrorCodes = new Dictionary<int,String>;
System.Reflection.FieldInfo[] fields = typeof(EDSDK).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
foreach (System.Reflection.FieldInfo field in fields) {
  EDSDKErrorCodes[(uint)field.GetValue(null)] = field.Name;
}

, :

MessageBox.Show(EDSDKErrorCodes[errorCode]);

errorCode - , , :

uint errorNumber = uint.Parse(errorCode.Substring(2), System.Globalization.NumberStyle.HexNumber);
0

All Articles