Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all

If you have constants, very often used variables (whatever the name is), what is the best way to store them ? does it make a difference ?

Code (static class, const variables, enum, ... etc.)
config file ( appsettings for example )
Database
Posted
Updated 16-Jul-14 1:39am
v2
Comments
Richard MacCutchan 16-Jul-14 7:41am    
In code. If they are stored in a file or database then they are not constants, but parameters.

1 solution

If they are indeed constant then you can use ... constants. Here is an example with a reasonably good explanation of when/why to use them http://www.dotnetperls.com/const[^]

If you have a set of related data then enums http://www.dotnetperls.com/enum[^] are useful and type-safe.

E.g.
C#
enum WorkingDays
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday
}
clearly shows that I don't work on Fridays (wishful thinking) and my working week starts on a Monday. It also means that the compiler will tell me if I do something silly like
WorkingDays wd = 6;
- which is better than finding out when my program is actually running.

If you have things that are relatively constant but may change in the future then AppSettings may be appropriate http://www.dotnetperls.com/settings[^]. This would be things like connection strings, file locations etc.

I have also used a Database to store "standing data" where there are lots of constant values or semi-constant where they change on a regular basis (e.g. ISA Limits, tax thresholds etc).

So you see there is not really an overall concept of "best" - it depends on the nature of the program you are writing, and you will have to use your judgement on each individual case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900