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 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
  • 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-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

Ask A Question

Stats

  • Questions 271k
  • Answers 271k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should only create the connection once since the signal… May 13, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer This page http://msdn.microsoft.com/en-us/library/ms175917.aspx has the shortcut documentation for the SQL… May 13, 2026 at 1:45 pm
  • Editorial Team
    Editorial Team added an answer You could add them as a query string or keep… May 13, 2026 at 1:45 pm

Related Questions

I have a winforms project which allows future developers to create plugins for the
I've implemented Wurfl, but the problem with this is that it's giving me the
I have an address class that uses a regular expression to parse the house
In my specific case, I am making use of a discriminator column strategy. This

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.