转换枚举以在类中使用

时间:2015-07-15 14:05:02

标签: c# enums

下面我有一段代码我已经做了很长时间了:

 enum SavingsIndicator
 {
   OKSavings,
   GoodSavings,
   GreatSavings,
 }

static void Main(string[] args)
{

 foreach (string line in gdsLines)
        {

            Match m = Regex.Match(line, @"^(?<date>[^|]+)\|.*Processed\s+(?<locator>[^.]+)\.{3}.*?Savings\s+found:\s*\$(?<savings>\d+\.\d+)\s*\(\s*(?<percent>\d+\.\d+)\s*%");
            if (m.Success)
            {
                Savings s = new Savings();
                s.Prefix = string.Empty;
                s.GDSDate = m.Groups["date"].Value.Trim();
                s.GDSLocator = m.Groups["locator"].Value.Trim();
                s.GDSDollarSavings = decimal.Parse(m.Groups["savings"].Value, CultureInfo.InvariantCulture);
                s.GDSPercentSavings = decimal.Parse(m.Groups["percent"].Value, CultureInfo.InvariantCulture);

                gdsSavings.Add(s);
                gdsTotalSavings += s.GDSDollarSavings;
                gdsTotalPercentage += s.GDSPercentSavings;
 if (s.GDSDollarSavings >= 500)
                {
                    s.Prefix = SavingsIndicator.GreatSavings;
                }
                else if (s.GDSDollarSavings >= 100 && s.GDSDollarSavings < 500)
                {
                    s.Prefix = SavingsIndicator.GoodSavings;
                }
                else
                {
                    s.Prefix = SavingsIndicator.OKSavings;
                }
            }

        }
        foreach (var savings in gdsSavings.OrderByDescending(savingsInstance => savingsInstance.GDSDollarSavings))
        {
            Console.WriteLine(savings.ToString());

        }
    }
   }

以下是我为此工作的课程:

class Savings
{
    public string Prefix { get; set; }
    public string GDSDate { get; set; }
    public string GDSLocator { get; set; }
   public decimal GDSDollarSavings { get; set; }
   public decimal GDSPercentSavings { get; set; }

   public override string ToString()
   {
       return Prefix + "\nDate: " + GDSDate + " \nPNR Locator: " + GDSLocator + " \nDollar Savings: $" + GDSDollarSavings + " \nPercent Savings: " + GDSPercentSavings + " % " + "\n";
   }
}

我遇到的问题是我有前缀的地方。我希望这些枚举值在我的类的前缀变量中,但我不能将我的枚举值转换为字符串。有没有办法成功地将其转换为字符串,还是有另一种方法来解决这个问题?

0 个答案:

没有答案