Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What's the best way to declare and manage constants variables in a web application?

Posted on 2015-02-02
11
Medium Priority
?
279 Views
Last Modified: 2015-02-02
Hi, I'm using vs2012, C# and asp.net

What's the best way to organize and keep all constants used in my application?  Should I create a class then in it, I would declare public properties with set and get?  How would I set the value, string/int, in these properties using set?  Also, declare a struct in side this class is okay?  And, should I make this class static?
Thank you.
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 40
ID: 40584429
What do you mean by a constant variable. There is a contradiction in there.
0
 

Author Comment

by:lapucca
ID: 40584478
Yes, you're correct about the contradiction.  Thank you for catching that.

i want to have a way to ref some int and strings at one place.  These will not be change inside the application but just used as reference for their values.  For example, cell number for each cell in a grid based on their column name.  strings with information that will be access in side the application.

It will make it easier to just keep it one place and changes are only done once there.  I hope that makes sense about what I'm asking for.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40584479
I typically create a static class named Constants. Within that class, I create nested classes based on how I want to categorize my constant values. For example, I might have the following:

public static class Constants
{
    public static class Integers
    {
        public static class Lengths
        {
            public const int FirstName = 10;
            public const int LastName = 30;
        }
    }

    public static class Strings
    {
        public static class Configuration
        {
            public const string ConfigSectionName = "MyApplication";
        }

        public static class RegexPatterns
        {
            public const string NonNumeric = "[^0-9]";
        }
    }
}

Open in new window


Now, this approach does break the "single class per file" practice, but it does so for the benefit of collocating similar (in purpose) values. This approach also makes referring to the constant a bit more involved:

e.g.

int firstNameLength = Constants.Integers.Lengths.FirstName;

Open in new window


...but it does add an element of readability to the code. I wouldn't suggest getting two deep with the class nesting if you decide to use this approach. I also tend to break up my Constants class into multiple namespaces. In this way, I have the same class multiple times, but in each place the constant values are different, and under whichever namespace I put the class it is intended to only apply to things that are within that namespace.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lapucca
ID: 40584494
Kaufmed, that is exactly what I'm trying to sort out.  I'm not sure about using the nested class approach.  My original thought is a static class but then problem how to group the related constant together.  I thought about using struct in side the class but you can't assign values to a struct during declaring.   But maybe just limit the class nesting to 2 levels might work?
0
 

Author Comment

by:lapucca
ID: 40584526
Kaufmed,
oops, got compile error when I use nested class.  Cannot declare instance members in a static class.  Please have a look, thank you.
const.txt
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40584533
I'm not sure what you'd gain by using a struct. It's more or less the same concept as a class, except that structs are expected to be smaller in size (less than 16 B).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40584542
What you have appears to be fine. Are you sure that the error is with that code?

Screenshot
0
 

Author Comment

by:lapucca
ID: 40584564
Yes, there is an error in the error list for each of the declaration like this one:
Error      1      'XXXXXXXXXX.AppConstants.GridColumnIndex.Edit': cannot declare instance members in a static class      

That's strange that it looks fine in your.
0
 

Author Comment

by:lapucca
ID: 40584569
that's strange.  I added static key word in front of const then removed it.   Now I compiled with no error.  Thank you.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40584581
Yeah, that is weird. A const is implicitly static. You usually get an error if you try to tag a variable with both:

Screenshot
0
 

Author Comment

by:lapucca
ID: 40584597
I think VS was cached error or something weird happened.  But it's cleared no, thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question