如何获取作为数组返回的枚举值的索引?

时间:2017-01-21 17:03:25

标签: c# asp.net

我对此代码有两个问题,

首先,当我打印它时,第6天不合适,它打印如下:

On Day1:Temperature was12
On Day2:Temperature was13
On Day3:Temperature was15
On Day4:Temperature was17
On Day5:Temperature was18
On Day7:Temperature was19
On Day8:Temperature was20
On Day9:Temperature was21
On Day6:Temperature was22
On Day10:Temperature was23
The Minimum Tempreature was 12,
The Maximum temperature was 23,
The average Temperature was 18,

第二个问题是,当我打印这些日子时,我想要包括最低温度和最高温度的日期,如下所示:

The Minimum Tempreature was 12, at Day 1
The Maximum temperature was 23,at Day 10

如何根据其值获取枚举的名称?

这是上面打印的代码,

此代码将枚举(天,温度)作为参数,并以相关温度和最小值,最大值和平均值打印天数....

// if user select February 


if (DropDownList1.SelectedValue.ToString()== "February")
    {
        ShowsTempreatures(FebruaryMonth.Day2);

    }
 // The method which print and shows temperatures and minimum and maximum...

private void ShowsTempreatures(Enum February)
{
    int Minimumtemperture = 40;
    int Maximumtemperture = 0;
    int total = 0;
    int averageTemperatures = 0;

    // Return the values of Enum as an Array

    Array januaryData = Enum.GetValues(February.GetType());

    for (int i = 0; i < januaryData.Length; i++)
    {
        //printing the Days and Temperatures of enum February

        Label1.Text += string.Format("On {0}:Temperature was{0:D}<br/>", januaryData.GetValue(i));

        //finding out minimum and maximum and average temperatures

        int MonthTemp = (int)januaryData.GetValue(i);
        if (MonthTemp<Minimumtemperture)
        {
            Minimumtemperture = MonthTemp;

        }
        if (MonthTemp>Maximumtemperture)
        {
            Maximumtemperture = MonthTemp;
        }
        total = total + MonthTemp;
    }
    int totaltempretures = januaryData.Length;
    averageTemperatures = (total / totaltempretures);

    // printing the minimum,maximum and average temperature

    Label1.Text += string.Format("The Minimum Tempreature was {0},<br/> The Maximum temperature was {1},<br/> The average Temperature was {2},<br/> ",
            Minimumtemperture, Maximumtemperture, averageTemperatures);
}
private enum FebruaryMonth
{

    Day1 = 12,
    Day2 = 13,
    Day3 = 15,
    Day4 = 17,
    Day5 = 18,
    Day6 = 22,
    Day7 = 19,
    Day8 = 20,
    Day9 = 21,
    Day10 = 23
}

2 个答案:

答案 0 :(得分:1)

考虑这个枚举:

public enum DayOfMonth
{
    Day2 = 2,
    Day1 = 1
}

以下是获取所有枚举的方法:

var days = Enum.GetValues(DayOfMonth.Day1.GetType()).Cast<DayOfMonth>().ToList(); 
Console.WriteLine(days[0]);

以下是如何通过其值获取枚举。这是一个简单的演员:

DayOfMonth day2 = (DayOfMonth)2;
Console.WriteLine(day2);

以下是如何订购它们:

var ordered = days.OrderBy(x => x).ToList(); // or you can use OrderByDescending
Console.WriteLine(ordered[0]);

答案 1 :(得分:1)

这是因为Enum的按其值排序,而不是按每个值的名称顺序排列。

您可以创建一个IComparer,它将提取每个值并对其进行比较。

如果我们将lexicographical order严格地比较枚举值名称,我们将减少一个新的错误。告诉我们Day10的错误在Day2之前,因为在字典顺序中,10小于2。因此,它导致我们尝试不同的方式。

另一种更准确的方法是,只提取枚举名称和子串只有数字部分,然后我们将它们解析为整数,然后应用比较。

class Program
{
    static void Main(string[] args)
    {
        Array januaryData = Enum.GetValues(typeof(FebruaryMonth));
        Array.Sort(januaryData, new FebruaryMonthSubstringComparer());
        for (int i = 0; i < januaryData.Length; i++)
        {
            string res = string.Format("On {0}:Temperature was{0:D}<br/>", januaryData.GetValue(i));
            Console.WriteLine(res);
        }
    }
}

public class FebruaryMonthSubstringComparer : IComparer
{
    public int Compare(object x, object y)
    {
        int monthX = int.Parse(Enum.GetName(typeof(FebruaryMonth), x).Substring(3)); // Naive parsing
        int monthY = int.Parse(Enum.GetName(typeof(FebruaryMonth), y).Substring(3));

        return monthX - monthY;
    }
}