Get int value from enum in C#

前端 未结 28 1925
臣服心动
臣服心动 2020-11-22 04:58

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.

public e         


        
28条回答
  •  渐次进展
    2020-11-22 05:43

    If you want to get an integer for the enum value that is stored in a variable, for which the type would be Question, to use for example in a method, you can simply do this I wrote in this example:

    enum Talen
    {
        Engels = 1, Italiaans = 2, Portugees = 3, Nederlands = 4, Duits = 5, Dens = 6
    }
    
    Talen Geselecteerd;    
    
    public void Form1()
    {
        InitializeComponent()
        Geselecteerd = Talen.Nederlands;
    }
    
    // You can use the Enum type as a parameter, so any enumeration from any enumerator can be used as parameter
    void VeranderenTitel(Enum e)
    {
        this.Text = Convert.ToInt32(e).ToString();
    }
    

    This will change the window title to 4, because the variable Geselecteerd is Talen.Nederlands. If I change it to Talen.Portugees and call the method again, the text will change to 3.

提交回复
热议问题