1

I am currently creating a console applications that only accepts some commands defined by me. The thing is I'm storing a lot of error and notification messages on a static class I created called Messages and then just calling them like Messages.ErrorMessage.... ErrorMessage is just a static string that contains w/e I want printed on the console. What I wanted to ask is if that's a good way of implementing such behavior or should I instead change where I'm keeping all of this Messages?

Thanks for your help!

Alonso Quesada
  • 125
  • 1
  • 3
  • 11
  • Static classes are in fact always bad behavior. Such design tends to become error-prone and has a lot of side effects. You can store the messages in a dedicated class. You create a instance of that class and it is uses as a parameter that is passed through the entire program. A **Singleton** instance is in many design patterns books seens as an anti-pattern. – Willem Van Onsem Jun 09 '14 at 09:47
  • 1
    If I understand correctly, you want a Resource file: http://stackoverflow.com/q/90697/7586 – Kobi Jun 09 '14 at 09:49

2 Answers2

2

I guess for your need you can use Resource file instead of static class.

as documented on official site

Visual C# applications often include data that is not source code. Such data is referred to as a project resource and it can include binary data, text files, audio or video files, string tables, icons, images, XML files, or any other type of data that your application requires. Project resource data is stored in XML format in the .resx file (named Resources.resx by default) which can be opened in Solution Explorer.

For more information :-

http://msdn.microsoft.com/en-us/library/7k989cfy(v=VS.90).aspx

Neel
  • 11,625
  • 3
  • 43
  • 61
  • So far I've being told to either use resource files or just create a regular class for handling messages... What's the best approach? – Alonso Quesada Jun 09 '14 at 09:55
  • @AlonsoQuesada have a look here http://stackoverflow.com/questions/1134018/what-are-the-benefits-of-resource-resx-files and http://stackoverflow.com/questions/6370921/best-place-to-store-static-error-strings?rq=1 – Neel Jun 09 '14 at 10:00
  • Ill try resource files then... thanks for your quick response and have a nice day :) – Alonso Quesada Jun 09 '14 at 10:02
0

As suggested by others, storing them in resource files is the recommended way to deal with such resources, particularly if you'll need to deal with internationalization. then you can load a different resource file from a satellite assembly at run time with the correct information.

You will notice a slight performance hit as the strings will be looked up in a resource dictionary, but usually that is negligible.

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61