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

  • Home
  • SEARCH
  • 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 263591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:36:52+00:00 2026-05-11T22:36:52+00:00

I have a Class named Constants that contains all the constant variable in my

  • 0

I have a Class named “Constants” that contains all the “constant” variable in my application (mostly Strings).

The Class is written like so:

public class Constants
{
     public const string DATABASE="myDatabase";
     public const string whatever="whatever";


     public enum Colors
     {
        Red
        Blue
        Orange
     }

     public const string Time = "07/03/2009 9:14 PM";
}

The members of this Class can be accessed normally by other classes.

The weird thing is, if I remove the “const”, that variable can no longer be accessed on other classes.

public class Constants
{
     public const string DATABASE="myDatabase";
     public const string whatever="whatever";


     public enum Colors
     {
        Red
        Blue
        Orange
     }

     public string Time = DateTime.Now.ToString(); //NO LONGER CONST
}

I tried to CLEAN the solution and rebuilding. I also closed/re-run VS2005. Is this a known bug? Or am I missing something else?

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-11T22:36:52+00:00Added an answer on May 11, 2026 at 10:36 pm

    Once you remove the const modifier the variable becomes an instance variable. Instance variables can only be accessed through an instance of the class (not through the type). This is “By Design”.

    You would need to use code like the following to access Time

    var c = new Constants();
    var t = c.Time;
    

    If “const” doesn’t suit your need for some reason, particularly if you are using a type which cannot be const, you may want to try static instead. It will have the same effect in terms of access semantics.

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

Sidebar

Related Questions

I have a class named CommonCode that is used for stocking all methods I
I am working with a Class that contains constants or parameter values that all
I have a class named toto which I send to a function that does
Where is the best place to create a class that contain all the application
I have a class that contains a collection. The two instances of the class
I have written the following code: @Entity @Table(name=person) @Inheritance(strategy=InheritanceType.JOINED) public class Person { private
i have class named Group i tried to test configuration: var cfg = new
I have a class named baseClass. From this class I inherit a class names
I have a class named Person and in this class is the property PersonName
I have a class named project with the following data members. class Project {

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.