tags:

views:

1860

answers:

4

I have this code;

using System;

namespace Rapido
{
    class Constants
    {
        public static const string FrameworkName = "Rapido Framework";
    }  
}

Visual Studio tells me: The constant 'Rapido.Constants.FrameworkName' cannot be marked static

How can I make this constant available from other classes without having to create a new instance of it? (ie. directly accessing it via Rapido.Constants.FrameworkName)

+13  A: 

A const is already static as it cannot change between instances.

ggf31416
Right... so I don't understand why the compiler balks when you explicitly denote it as such...
Cuga
what is there to understand? That's the way its implemented!
Mitch Wheat
I understand that's the way it's implemented. I'm asking why it's done that way.
Cuga
Take care not to confuse const and static, they mean different things. const refers to an item's value whereas static refers to how an items storage is allocated. See http://stackoverflow.com/questions/842609/why-does-c-not-allow-const-and-static-on-the-same-line/842649#842649
Tim Long
A: 

Make the class and the field public, like so:

public class Constants
    {
        public static const string FrameworkName = "Rapido Framework";
    }

Set the permissions appropriate to how you wish the code to be exposed. If you want this method available publicly, make it so. In C#, classes are restricted to internal by default.

Cuga
you've accepted it, but it doesn't compile!
Mitch Wheat
see my answer below....
Mitch Wheat
@Mitch If this is a class to only store constant values, then it does make sense to declare the class as static (and final for that matter), but I don't understand why you don't think it should compile. How so?
Cuga
Plus it should still compile whether the class is static or not. What do you mean, Mitch?
Cuga
You can't declare a field as static and constant. Remember that Constants are copied at compile time. And if you ever change the value later you will only affect Assemblies that are recompiled with the new constant value.
Matthew Whited
@Cuga: please create an empty project, and try to compile your code...
Mitch Wheat
The C# compiler must be busted then. Better write your own.
swilliams
LMAO. damn... Anders must have screwed up.
Matthew Whited
Yup, you're right. `const` and `static` should not be on the same line. This is why I went back to Java.
Cuga
"This is why I went back to Java" - that seems sane.
Mitch Wheat
Haha. You're an MVP-- I wouldn't expect any different from you. I'm not a hater of Microsoft. I did C# for 18 months... Their current offerings are top-notch. Yet I feel like they're out to abstract the developer away from the actual code... I personally have a hard time with this b/c I like to be down in the nitty-gritty.
Cuga
@ Cuga: I can assure you that my being an MVP doesn't come in to it. MVPs still have an independent voice. I am highly critical of some MS products, whilst really get behind others (such as SQL Server)
Mitch Wheat
Jon Skeet is an MVP. He used to work in Java I believe!
Mitch Wheat
This was the first answer that worked for me, which is why I accepted it.
Charlie Somerville
@Cuga, its not that hard to file up the c# compiler and confirm your code compiles. Please correct your answer.
Sam Saffron
I can't edit or even delete this post as long as it's the accepted answer.
Cuga
+17  A: 
public static class Constants
{
    public const string FrameworkName = "Rapido Framework";
}
Mitch Wheat
I think *this* is actually the right way of doing it.
Hemant
It is! .........
Mitch Wheat
Sorry I was reading it wrong. But why would you do this? This class would effectively contain nothing. And if you were to ever change this value you would have to recompile everything. It would be better to have this be a static readonly field.
Matthew Whited
I over looked you static on the class definition. I thought you did the same as the answer that has been accepted.
Matthew Whited
The poster asked for a way of not instantiating a class, and have it contain constants.
Mitch Wheat
You wouldn't have to create an instance to get to the constant value. Constants work just like statics. Other than they are copied at compile time instead of referenced.
Matthew Whited
+4  A: 

You don't need to declare it as static - public const string is enough.

Andrew Kennan
In fact it is an error to declare it static because that would imply that memory allocation and runtime initialisation needs to take place, neither of which is needed for a constant.
Tim Long