tags:

views:

208

answers:

4

Hi All,

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!

+9  A: 

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.

JaredPar
+2  A: 

The const keyword implies both static and readonly. Without const it's not static anymore, so you need an instance of Constants to access that member:

var c = new Constants();
Console.WriteLine(c.Time); // should work now
Thomas Freudenberg
IF I add Static, that would "fix" it right?
Ian
Const does NOT imply static readonly. Const means that the compiler will copy the field value into all places that it referenced. Static readonly implies that there is only one unmodifiable value in the assembly, which may be referenced throughout.
Steve Guidi
Yes, Time would then be accessible without an instance.
Thomas Freudenberg
Steve, you're right. However, IntelliSense-wise there's no difference ;)
Thomas Freudenberg
+1  A: 

When you remove the const modifier from the declaration of a variable, the variable becomes and instance-level declaration; const members are considered static members. Thus, you need to create an instance of your class to access the variable.

Thus,

Constants c = new Constants();
Console.WriteLine(c.Time);

Alternatively, you can add the static and readonly modifiers to say

class Constants {
    [...]
    public readonly static string Time = DateTime.Now.ToString();
}

Console.WriteLine(Constants.Time);
Jason
Const does NOT imply static readonly. Const means that the compiler will copy the field value into all places that it referenced. Static readonly implies that there is only one unmodifiable value in the assembly, which may be referenced throughout.
Steve Guidi
From the ECMA 334 spec, "[c]onstants are considered static members." See page 278. I agree with you that there is a semantic difference between const and readonly.
Jason
A: 

Instead of creating an instance of your Constants class, declare the class as "static":

public static class Constants 
{ 
  public string Whatever="whatever";
  // ...
}

You will still need to declare your "variables" as static but you can access them through the class: Constants.Whatever.

Hans Kesting