Class; Struct; Explain the confusion, which is better?

I have 46 lines of information, 2 columns each line ("Code number", "Description"). These codes are returned to the customer based on the success or failure of their initial sending request. I do not want to use the database file (csv, sqlite, etc.) for storage / access. The closest type that I can think of how I want these codes to be shown to the client is the exception class. Correct me if I am wrong, but from what I can say, enumerations do not allow strings, although such a structure seemed to be the best option initially based on how it works (for example, 100 = "missing name in request").

Thinking about this, creating a class might be the best modus operandi. However, I would appreciate more experienced advice or guidance and input from those who might be in a similar situation.

This is currently 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;
    }

}
+3
source share
5 answers

Both class and enumeration will be best. Then you can have more descriptive identifiers than "201".

The structure will also work, but they are more difficult to implement correctly, so you should stick to the class if you do not need the structure for any reason.

, Message. A switch - ( ), .

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

}

:

ReturnCode code = new ReturnCode(ReturnIdentifier.Success);

- , , :

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

( , . Message default .)

+4

, ( ) - . , Dictionary<int, string> .

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

RetrieveMessage:

return _dict[value];
+3

, ?

0

, .

    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

( Reflection), , , , Enums With Custom Attributes . , , DescriptionAttribute. - :

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());
}
0

Source: https://habr.com/ru/post/1744896/


All Articles