Search code examples
c#static-classes

Creating a set of static constant variables in c#


I am trying to make a set of variables that I can pass as method variable something like this

static class ListViewStates
{
    public const int ACCOUNTVIEW = 1;
    public const int LARGEIMAGEVIEW = 2;
    public const int INFOVIEW = 3;
}

class ListViewHandler
{
    public void SetListViewState(ListViewStates state)
    {
        switch(state)
        {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }
}
class main
{
    ListViewHandler listhndlr = new ListViewHandler();
    listhndlr.SetListViewState(ListViewStates.ACCOUNTVIEW);
}

What do I have to do with ListViewStates to make this work ?

I have looked around google but I can't seem to find the right search phrase to find an answer.


Solution

  • You have to Use enum instead of a seperate class. the definition for enum will be as follows:

    enum ListViewStates
    {
        ACCOUNTVIEW = 1,
        LARGEIMAGEVIEW = 2,
        INFOVIEW = 3,
    }
    

    To access this you have to rewrite Method signature as like the following:

    public void SetListViewState(ListViewStates state)
    {
        switch (state)
        {
            case ListViewStates.ACCOUNTVIEW:
                break;
            case ListViewStates.LARGEIMAGEVIEW:
                break;
            case ListViewStates.INFOVIEW:
                break;
        }
    }