5

How can I built a group of constant variable in c#?

For example :

IconType {
    public constant string folder = "FOLDER";
    public constant string application = "APPLICATION";
    public constant string system = "SYSTEM";
}

Then I need to use it like this ways IconType.system but I don't want to do declaration like IconType type = new IconType(), I want to direction access to its variable.

It just looks like JOptionPanel in java, when I want to display the icon I just need to call this JOptionPane.WARNING_MESSAGE

overshadow
  • 958
  • 2
  • 15
  • 31

5 Answers5

16

Just define them in a class, and since const are implicitly static you can use them

class IconType
{
    public const string folder = "FOLDER";
    public const string application = "APPLICATION";
    public const string system = "SYSTEM";
}

Later you can use them like:

Console.WriteLine(IconType.folder);

You may see webarchive page: Why can't I use static and const together? By Jon Skeet

Heavy JS
  • 51
  • 7
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    So many duplicate answers, but only one that provides the delicious 'implicitly static' detail. – Gusdor Oct 01 '13 at 13:50
  • Thanks you @Habib, I think this answer is more suitable for my situation although others answer might work as well. – overshadow Oct 01 '13 at 14:00
  • @overshadow, you are welcome, you may also use pascal casing for your `const`. See this question for [.Net naming conventions for const](http://stackoverflow.com/questions/242534/c-sharp-naming-convention-for-constants) – Habib Oct 01 '13 at 14:02
  • I had try this before but I put a public key world before the class IconType but it does not work. Do you know why? Why can't I put a public before the class? – overshadow Oct 01 '13 at 14:02
  • I also prefer making the class static; makes the intent more clear – Leyu Oct 01 '13 at 14:12
  • Sure this perfectly answers the question to build "a group of constant variable". But as it seems that overshadow does need the string values, but is going to use it enum-like - why not use just an enum? – dasheddot Oct 01 '13 at 14:18
  • @overshadow, you can put `public` in front of class, making it public. Could you explain what doesn't work, it should compile fine – Habib Oct 01 '13 at 14:23
  • @Habib Only put public class IconType it just can't access the variable, I also don't know why. If I add in the static key world then it works perfectly (public static class IconType). – overshadow Oct 01 '13 at 14:34
  • @overshadow, this should work without static, although having it as static is even better since you don't have anything else in it. But I just tried it in a simple console app and it seems to be working – Habib Oct 01 '13 at 14:44
1

Seems like you would like to use enums?

public enum IconType {
    Folder,
    Application,
    System
}

Wouldn't that not be enough?

dasheddot
  • 2,936
  • 4
  • 26
  • 33
1

You need a class.

   public static class IconType
    {
        public const string folder = "FOLDER";
        public const string application = "APPLICATION";
        public const string system = "SYSTEM";
    }
Gusdor
  • 14,001
  • 2
  • 52
  • 64
0

You could use an Enum instead?

MSDN Enumeration Types

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
0

You could build a class with consts, which are implicitly static, and thus accessible without an instance of the type.

class IconType
{
    public const string folder = "FOLDER";
    public const string application = "APPLICATION";
    public const string system = "SYSTEM";
}

You could also use the strong-typed Settings created for you by Visual Studio in most C# projects.

Properties.Settings.Default

Michael
  • 1,803
  • 1
  • 17
  • 26