-1

Now:

2 classes each with the same constant name, e.g. ERR123

class1 got public const int ERR123 = 123;

class2 got public const string ERR123 = "Error 123, something went wrong.";

So I call it like

int code = class1.ERR123;
string message = class2.ERR123;

I don't like this approach because I need to copy the constant names and touch 2 files when I add/change something.

I'd like to access it like that:

int code = Errors.Subcategory1.ERR123.Code; 
string message = Errors.Subcategory1.ERR123.Message;

and the declaration shouldn't be that inconvenient. Is this possible? Maybe using some reflection-magic?

UNeverNo
  • 549
  • 3
  • 8
  • 29

2 Answers2

1

To descript message codes with descriptions you can use enum with Descrition attribute. Get Enum from Description attribute

Community
  • 1
  • 1
IL_Agent
  • 707
  • 3
  • 16
0

Why make something more complex than it needs to be?

public class Errors
{
     public static class IntegerErrors {
          public const string ERR123 = 123;
      }

     public static class StringErrors {
         public const string ERR123 = "Error 123, something went wrong.";
     }
}

...

int i_value = Errors.IntegerErrors.ERR123;
string s_value = Errors.StringErrors.ERR123;
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39