2014-01-08 69 views
-1

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

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) 
{ 
} 

那么这将是这样做的最有效的方式?提前致谢。

+2

为什么不建立一个类来保存这些值,创建一个类的实例并赋值,然后将该对象传递给该方法? – Alan

+1

复制和粘贴工作得很好。 – Dirk

+0

例如我私下需要的WStoneCost无效StoneBuy ...等他们每个人 –

回答

0

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

如果你想最快的方式,写它是汇编程序设计语言。如果你想要一个可用的程序,一个简单易读的代码库,你应该根据面向对象的原则构建你的代码,而不仅仅是临时的。

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

+0

'写它是汇编程序设计语言'?你是认真的吗? – meda

+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 
    } 
} 
+0

谢谢,但在这种情况下,要么我应该从一个类/方法导入到另一个很多我的意思是像这些小数所需的随机数(因为我必须重新运行随机的一代,但保留这些成本。)这就是为什么我不想在一开始就这样做。 –

+0

我不明白你为什么要计算'WStoneCost'两次?为什么 – meda

+0

按下按钮后,每日价格变化,从而产生两个随机数,每个只产生价格的百分比,然后将所有其他百分比来源放在一起,并产生价格。 –

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,则只需将新值添加到PriceMethodenum,并且上面的循环将其拾取。没有在多个地方输入相同的东西。

相关问题