2

I have a newb question. I have a winforms application that has a number of classes that referenced a number of UNC network paths. I started to notice I had a bunch of string duplication and then started trying to weed them out by consolidating them into the classes that just make more sense to have them. I was then referencing the class with the string I needed each time I needed to get the value of the string but I'm sure this was a sloppy way to do it.

Now I've settled on making a single class ("StringLibrary") and am referencing that class in each class I need to pull strings from. This seems much more efficient than what I was doing before, however, I'm still not sure if this is a good way to do it in general.

Is there a better way (i.e. more standardized way) to consolidate a group of strings or values in c#?

Residualfail
  • 79
  • 1
  • 12
  • Can you give some example code? – Maarten May 01 '14 at 13:01
  • 4
    Strings that you end up showing to users may usually end up going into resource files (and then .NET tooling actually lets you access them similarly to what you're doing). Strings for e.g. paths would normally be read from a config file rather than being hard-coded. – Damien_The_Unbeliever May 01 '14 at 13:02

2 Answers2

3

It depends on whether the strings are configuration or more permanent. For network paths, you may want to put them in your app.config file (see What is App.config in C#.NET? How to use it?), since they may change from time to time, or differ between deployments (and you do not want to recompile your code for every site) Depending on the nature of the data, you may alternatively want to store it in the registry or in a database.

If it is something more tightly tied to your code, like names of controls on a form, or names of columns in your database. Then you may want to centralise their definitions as you suggest, and reference them all from there. When there are a lot of them, your may want to split your StringLibrary into more classes with more relevant names (e.g. if you are speficying names of columns in your database, then you may want to create such a static class for each table in your database) If you take this approach, and since you are new to C# it may also help to read Static readonly vs const to decide if you want them to be const or static readonly.

Community
  • 1
  • 1
Nameless One
  • 1,615
  • 2
  • 23
  • 39
  • Thank you all for your feedback. I had considered making an external .ini file for this as well but I was unaware of the app.config file. I think the app.config file will be my best approach. Thank you all again! – Residualfail May 01 '14 at 14:45
2

These could be added to an application config file/ web config file, resource files and/or settings files.

This way you can administer these strings, should they change, without having to re-build your application and also apply transformations (if in an app.config/web.config) when performing releases to different environments/deployments.

brumScouse
  • 3,166
  • 1
  • 24
  • 38