Best way to store / search values โ€‹โ€‹of a pair of values

I have a list of error codes that I need to reference, sort of like:

Code / Error Message  
A01 = whatever error  
U01 = another error  
U02 = yet another error type

I get the code returned to me through a web service call and I need to display or get a readable error. Therefore, I need a function when passing code that returns a readable description. I was just about to make an elective case, but thought it might be the best way. What is the best way / most efficient way to do this?

+3
source share
2 answers

Use a dictionary (in C #, but the concept and classes are the same):

// Initialize this once, and store it in the ASP.NET Cache.
Dictionary<String,String> errorCodes = new Dictionary<String,String>();

errorCodes.Add("A01", "Whatever Error");
errorCodes.Add("U01", "Another Error");


// And to get your error code:

string ErrCode = errorCodes[ErrorCodeFromWS];
+8
source

. , . , , , , , , XML slimier. , , - :

public static class ErrorCodes
{
    private static Dictonary<string, string> s_codes = new Dicontary<string, string>();
    static ErrorCodes()
    {
         s_codes["code"] = "Description";
         s_codes["code2"] = "Description2";
    }

    public static string GetDesc(string code)
    {
         return s_codes[code];
    }
}

, , .

0

All Articles