从一个方法到另一个方法获取大量变量#

时间:2014-01-08 20:40:56

标签: c# variables methods decimal

我遇到了这个问题。假设我们有大约20个变量,每个变量都包含其他值(所有这些变量都是小数),现在我想尽可能快地从一个方法到另一个方法。什么是实现目标的最快方式? 这是一些代码:

private void button2_Click(object sender, EventArgs e)
{
  decimal WStoneCost = (PriceMethod.StoneCost * AP) / 100;
  decimal WWoodCost = (PriceMethod.WoodCost  *AP)/100;
  decimal WMetalCost = (PriceMethod.MetalCost  *AP)/100;
  decimal WSteelCost = (PriceMethod.SteelCost  *AP)/100;
  decimal WPaperCost = (PriceMethod.PaperCost  *AP)/100;
  decimal WPotatoeCost = (PriceMethod.PotatoeCost  *AP)/100;
  decimal WTomatoeCost = (PriceMethod.TomatoeCost  *AP)/100;
  decimal WCucumberCost = (PriceMethod.CucumberCost  *AP)/100;
  decimal WCornCost = (PriceMethod.CornCost  *AP)/100;
  decimal WFlourCost = (PriceMethod.FlourCost  *AP)/100;
  decimal WBreadCost = (PriceMethod.BreadCost  *AP)/100;
  decimal WSwordsCost = (PriceMethod.SwordsCost  *AP)/100;
  decimal WShieldsCost = (PriceMethod.ShieldsCost  *AP)/100;
  decimal WCannonsCost = (PriceMethod.CannonsCost  *AP)/100;
  decimal WRiflesCost = (PriceMethod.RiflesCost  *AP)/100;
  decimal WBulletsCost = (PriceMethod.BulletsCost  *AP)/100;
  decimal WCowCost = (PriceMethod.CowCost  *AP)/100;
  decimal WhorseCost = (PriceMethod.horseCost  *AP)/100;
  decimal WSheepCost = (PriceMethod.SheepCost  *AP)/100;
  decimal WChickenCost  = (PriceMethod.ChickenCost *AP)/100;
  decimal WPigCost = (PriceMethod.PigCost * AP) / 100;
}

我想将每个小数都移动到不同的方法,如下所示:

private void StoneBuy_Click(object sender, EventArgs e)
{
}

那么最有效的方法是什么呢?提前谢谢。

3 个答案:

答案 0 :(得分:3)

创建一个包含所有属性的类

public class MyClass
{
    decimal WStoneCost = (PriceMethod.StoneCost * AP) / 100;
    decimal WWoodCost = (PriceMethod.WoodCost  *AP)/100;
    decimal WMetalCost = (PriceMethod.MetalCost  *AP)/100;
    decimal WSteelCost = (PriceMethod.SteelCost  *AP)/100;
    decimal WPaperCost = (PriceMethod.PaperCost  *AP)/100;
    decimal WPotatoeCost = (PriceMethod.PotatoeCost  *AP)/100;
    decimal WTomatoeCost = (PriceMethod.TomatoeCost  *AP)/100;
    decimal WCucumberCost = (PriceMethod.CucumberCost  *AP)/100;
    decimal WCornCost = (PriceMethod.CornCost  *AP)/100;
    decimal WFlourCost = (PriceMethod.FlourCost  *AP)/100;
    decimal WBreadCost = (PriceMethod.BreadCost  *AP)/100;
    decimal WSwordsCost = (PriceMethod.SwordsCost  *AP)/100;
    decimal WShieldsCost = (PriceMethod.ShieldsCost  *AP)/100;
    decimal WCannonsCost = (PriceMethod.CannonsCost  *AP)/100;
    decimal WRiflesCost = (PriceMethod.RiflesCost  *AP)/100;
    decimal WBulletsCost = (PriceMethod.BulletsCost  *AP)/100;
    decimal WCowCost = (PriceMethod.CowCost  *AP)/100;
    decimal WhorseCost = (PriceMethod.horseCost  *AP)/100;
    decimal WSheepCost = (PriceMethod.SheepCost  *AP)/100;
    decimal WChickenCost  = (PriceMethod.ChickenCost *AP)/100;
    decimal WPigCost = (PriceMethod.PigCost * AP) / 100;
}

让您的活动成为您班级的财产:

public class Whatever
{
    public MyClass Mine;

    public Whatever()
    {
        Mine = new MyClass();
    }

    private void button2_Click(object sender, EventArgs e)
    {
         Mine.WStoneCost = (PriceMethod.StoneCost * AP) / 100;
         //etc
    }

    private void StoneBuy_Click(object sender, EventArgs e)
    {
        //Use it here somehow
    }
}

答案 1 :(得分:0)

如果你想要一起处理这些变量,那些声音就像它们在一起一样。如果它们属于一体,并且你想要对它们进行整体操作。那听起来就像你应该在那里上课一样。也许您应该将所有这些数据放入一个类中,因此您只需在方法之间“移动”时传递对它的引用。

如果你想要最快的方式,写它是汇编编程语言。如果你想要一个可维护的程序,一个易于阅读的代码库,你应该根据OO原则构建代码,而不仅仅是ad-hoc。

请参阅Dave Zych的解决方案,了解可行的方法。

答案 2 :(得分:0)

我使用字典:

// Note: assuming that PriceMethod is an enum.
var costs = Dictionary<PriceMethod, decimal>();
foreach (PriceMethod priceMethod in (PriceMethod[])Enum.GetValues(typeof(PriceMethod)))
{
    // Assuming AP is a decimal, since if it's an int there might be rounding issues.
    costs[priceMethod] = ((int)priceMethod * AP) / 100;
}

然后,您可以将字典保存为私有字段,以便在您想要的任何类方法中使用。此外,如果您添加新的PriceMethod,则只需将新值添加到PriceMethod enum,然后上面的循环就会选中它。没有在多个地方打出相同的东西。