Skretch
Skretch

Reputation: 69

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.

Upvotes: 2

Views: 89

Answers (2)

sujith karivelil
sujith karivelil

Reputation: 29026

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;
    }
}

Upvotes: 4

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186748

Probably you want a enum, not a static class:

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

Upvotes: 3

Related Questions