3

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;
    }

}

5 Answers 5

4

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 Message property. A switch is implemented using a hash table (if there are five values or more), so the lookup is very fast.

public enum ReturnIdentifier {
  Success = 100,
  MissingName = 201;
}

public class ReturnCode {

  public ReturnIdentifier Code { get; private set; }

  public ReturnCode(ReturnIdentifier code) {
    Code = code;
  }

  public string Message {
    get {
      switch (Code) {
        case ReturnIdentifier.Success:
          return "Request completed successfuly.";
        case ReturnIdentifier.MissingName:
          return "Missing name in request.";
        default:
          return "Unexpected failure, please email for support.";
      }
    }
  }

}

Usage:

ReturnCode code = new ReturnCode(ReturnIdentifier.Success);

If you get an integer code from somewhere, you can still use it as the enumerator values correspond to the codes:

int error = 201;
ReturnCode code = new ReturnCode((ReturnIdentifier)error);

(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 Message value, it will end up in the default case as the value doesn't match any of the other cases.)

1
  • This is what I was looking for. You where clear in your reasoning and provided straightforward examples. I could not have asked for a better response. Thank you immensely for the help.
    – Riana
    May 11, 2010 at 13:18
3

I think choosing a class(as you did) is a good decision. You can make the code a little more compact and readable, if you use Dictionary<int, string> for mapping codes to descriptions.

_dict.Add(100, "Description1");
_dict.Add(201, "Description2");
...............................

And RetrieveMessage:

return _dict[value];
0

How about deriving from Dictionary, or storing the data table in code using a Dictionary field that you can index into?

0

Maybe a dictionary based approach would look more elegant.

    private static Dictionary<int, string> errorCodes = 
    new Dictionary<int, string>()
    {
        {100, "Request completed successfuly"},
        {200, "Missing name in request."}
    };

    private string RetrieveMessage(int value)
    {
        string message;
        if (!errorCodes.TryGetValue(value, out message))
            message = "Unexpected failure, please email for support";

        return message;
    }
0

It will definitely be more slower (since it uses Reflection) but speaking of being compact, I think Enums With Custom Attributes is appropriate for this need. Please continue reading the comments since DescriptionAttribute is mentioned there. Something like;

public enum ErrorMessage
{
    [System.ComponentModel.Description("Request completed successfuly")]
    Success = 100,
    [System.ComponentModel.Description("Missing name in request.")]
    MissingName = 201
};

public static string GetDescription(this Enum en)
{
    Type type = en.GetType();

    System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());

    if (memInfo != null && memInfo.Length > 0)
    {
        object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute),
                false);

        if (attrs != null && attrs.Length > 0)
            return ((System.ComponentModel.DescriptionAttribute)attrs[0]).Description;
    }  

    return en.ToString();
}

static void Main(string[] args)
{
    ErrorMessage message = ErrorMessage.Success;
    Console.WriteLine(message.GetDescription());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.