Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a group of constant variables in c#

Tags:

c#

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

like image 331
overshadow Avatar asked Oct 01 '13 13:10

overshadow


2 Answers

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: Why can't I use static and const together? By Jon Skeet

like image 60
Habib Avatar answered Sep 20 '22 02:09

Habib


Seems like you would like to use enums?

public enum IconType {
    Folder,
    Application,
    System
}

Wouldn't that not be enough?

like image 26
dasheddot Avatar answered Sep 17 '22 02:09

dasheddot