Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:10:12+00:00 2026-05-26T00:10:12+00:00

In all applications, there are always some constant values that are used. I don’t

  • 0

In all applications, there are always some constant values that are used.

I don’t like hardcoding these values in my application in each code as it reduces maintainability significantly.

I have 2 options:

  1. Create an internal Constants class within your target class and add public const values such as

    internal static class Constants
    {
    public const string IdAttribute = “id”;
    }

or

  1. Create a Resource file (.resx) and store my constant values and formulas there.

I have used the second approach in my application here. One issue I had with using a resource file was that I could not use .NET 4.0 Optional Parameter feature with them.

e.g. the below doesn’t work:

public void DoSomething(string id = Resources.Constants.Id)
{
// ..
}

The error was that the value of Id is not determined at runtime.

Basically, which way would you use and recommend to manage your constants for each class? why?

My current approach is that if I have constants that should be used inside one class only, I create a static Constants class inside that class. If my constants are to be used in more than one class or project, I’ll put them in a resource file e.g. PageUrls.resx.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:10:13+00:00Added an answer on May 26, 2026 at 12:10 am

    Personally I’d rather have constants defined in the classes where they logically belong, in a similar way to (say) DateTime.MinValue.

    Then, in general making these constants public is a bit of a “code smell”, implying some coupling that should probably be expressed in a better way. There’s obviously no harm in making them private (or internal) when your code allows.

    Sometimes there are overall constants that belong globally, however. These are often actually configuration and should be treated as such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there some way to terminate all (Java) applications launched with Eclipse at once?
In my web application there is a process that queries data from all over
In pretty much all applications that have a menu bar, some of the items
Morning all, I design a number of compact framework applications, and something that always
Is there any way to make all the applications on a server use SQL
I have three applications that talk to each other using sockets. They can all
During the course of our application login there are several queries ran, all around
Is there an easy way to log all exceptions in an ASP.NET application? I'm
UPDATE: All of these problems was for converting a Website application to a Web
We have an application where users enter prices all day. These prices are recorded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.