display int value from enum

This question already has an answer here:

  • Get int value from enum in C# 24 answers

  • To get all values of the Enum try this

    var allValues = Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray();
    

    and your totalSteps property should look like this

    public int[] totalSteps
    {
       get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); }
    }
    

    how can I return it in "get" function? the value cannot be converted

    Property (I guess) is defined to get/set selected enum value, so you can't return an int when type is TotalSteps.

    I suggest have another readonly property inside Step which converts selected enum value to an int

     public int Step
     {
         get {return (int)totalsteps; }
     }
    

    Since you mentioned (as a comment) you want all int values to bind to ComboBox do this.

     public List<int< ComboValues
     {
         get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToList(); } 
     }
    

    you should make a property which return an int instead of TotalSteps

    u are doing this

        private TotalSteps totalSteps;
    
        public TotalSteps totalsteps
        {
            get { return totalSteps; }
            set { value = totalSteps; }
        }
    

    and I would suggest to do this

        private TotalSteps totalSteps;
        private int totalStepsInInt;
    
        public int TotalstepsInInt
        {
            get { return totalStepsInInt; }
            set { totalStepsInInt = value; }
        }
    

    and while setting this property u have to convert totalSteps in int by doing this.

       `TotalStepsInInt = (int)totalSteps;`
    
    链接地址: http://www.djcxy.com/p/91794.html

    上一篇: 如何从System.Enum转换为基本整数?

    下一篇: 从枚举中显示int值