1

I want to separate my application messages. The end result would look something like this:

ApiMessages.Error.UserNotFound
ApiMessages.Warning.SomeWarning
ApiMessages.Info.SomeInfo
etc.

I thought it would be a piece of cake but I realize I don't know how to do this... If I had a static class called Error with multiple static readonly string properties that would work fine.. but what if I want a collection of such classes that I can easily access and have all my messages sorted out nicely? How could I do this?

Angelo
  • 183
  • 1
  • 1
  • 10
  • *but what if I want a collection of such classes*. Why doesn't a static class containing all strings work for you? You do know that you can create in VS resource classes that enable you to use a design time interface to set all your literal strings and creates a class similar to what you want for you? – InBetween Aug 27 '20 at 16:52
  • I actually don't know that, can you elaborate/give a link? Edit: wait, you mean .resx files? – Angelo Aug 27 '20 at 16:55
  • Messages for users do not belong in string constants, as this makes it impossible to internationalize your application. The proper location is in [resource files](https://stackoverflow.com/questions/24117580/best-place-to-store-saved-messages). – John Wu Aug 27 '20 at 16:56
  • @JohnWu Yes, maybe I should do that, although to be fair I only plan this to be in english... – Angelo Aug 27 '20 at 17:00

1 Answers1

1

One way to accomplish what you're asking for is to create the classes that contain the constants inside another class:

public static class ApiMessages
{
    public static class Error
    {
        public const string UserNotFound = "Error: User not found.";
    }

    public static class Warning
    {
        public const string SomeWarning = "Warning! This action is frowned upon.";
    }

    public static class Info
    {
        public const string SomeInfo = "Only 3 countries don't use the metric system.";
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Wow, yes, this does look like what I want. I ended up using a .resx file, but thanks for the solution – Angelo Aug 28 '20 at 09:29