Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best practice with constants 2

Status
Not open for further replies.

Craftor

Programmer
Feb 1, 2001
420
0
0
NZ
Hi all

Just a quick question on best practices. I have a legacy VB6 project that I'm converting to C# 2.0. There are a lot of constants littered throughout the various VB modules - I would like to consolidate them into one central location.

Would it be a good idea to create a project - MyProject.Constants and seperate them all out into class files within the project so I can access them as MyProject.Constants.General, MyProject.Constants.Internet etc. etc. or is there a better way of doing this?

Thanks as always

Craftor
:cool:
 
I wasn't talking about app.config. But in a file of type settings like provided by the framework. If you click on new it is one of the many file types. It has a typical hashmap structure (key/value pairs) with one difference that you can specify if they are application or user settings. User settings can be altered by the user at runtime application are readonly and can only by changed at design time. And you can have more then one of these files.

At least 100 isn't all that much. BTW it is good practice to remove all string from your app and put them in a settings file.

Plus I think it is stored in xml.

Christiaan Baes
Belgium

"My new site" - Me
 
Hi Christiaan

I don't think the settings file will work for my project as the settings need to be accessed across all projects in the application.

Is there another way I could do it?

Thanks for all your help.


Craftor
:cool:
 
In C#, there is no global data, every thing need to be in types. Even if you store these values in an app settings file, XML file or even a plain text file, it will still be your responsibility to extract these values from wherever and assign them to the symbols in your code, the media you used to store the values are only for that purpose (data store) and there is no way to force the compiler to substitute the value of the symbol when it sees it in the code. If you want an object oriented way of doing it, you need to group these constants by semantics and create enumerators to encapsulate the constants, then in your code search for each constant and replace it by EnumName.Constant.

Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top