const values accessible in namespace?

I have two classes, in one namespace:

namespace MySpace

{

public interface IMyClass1

{

void doSomething();

}

public class MyClass : IMyClass

{

void DoSomething();

}

public AnotherClass

{

void DoSomethingElse();

}

}//end namespace MySpace

What I want to be able to do is have a const value accessible to both classes in the MySpace namespace. When I put 'public const long maxVal;' just below the namespace declaration, the IDE doesn't recognize it, so it must not be allowed.

The values are for limiting a vlaue to a certain amount. can I andshould I use constants? It would be nice if I could make the values accessibly publiclly through the interface as well, but I'm reaching here...

[822 byte] By [TheAdmiral] at [2007-12-29]
# 1
You need to make the constants public and static. You could, if you wanted, create a static Constants class:

public static Constants
{
public static int MY_FIRST_CONSTANT = 1234;
...
}

And then reference the values like: int x = Constants.MY_FIRST_CONSTANT;

MatthewWatson at 2007-9-5 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Hello,

When you define a static value for sharing, it can't be modified by "const". Instead, you can use "readonly", if you dont want it to be changed at run time.

And here is the difference between const and readonly modifier:

'const':

  • Can't be static.

  • Value is evaluated at compile time.

  • Initiailized at declaration only.

'readonly':

  • Can be either instance-level or static.

  • Value is evaluated at run time.

  • Can be initialized in declaration or by code in the constructor.

According to your need, just define a static readonly variable:

public static readonly val;

Thank you

FigoFei-MSFT at 2007-9-5 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3

Also,

you could define the value as a const i.e. public const int x; since consts are also trated as static variables, however they do have one difference to static variables in that the actual value of the const variable is replaced in the code where it is called from, so if you define a const value in assembly A then reference that value in assembly B, if you change the value of the const in assembly A you will have to recompile assembly B again otherwise it will still have the old value.

Mark.

MarkDawson at 2007-9-5 > top of Msdn Tech,Visual C#,Visual C# Language...