2013-09-16 57 views
-4

我只是不能得到這種方法的工作,我不知道爲什麼。創建一個多輸出的方法

是視覺給我的錯誤是:

錯誤1命名一個局部變量「laborCost」不能在此範圍中聲明,因爲它會給予不同的意義,這是已經在使用「laborCost」,一個「父母或電流」的範圍來表示別的

,這就是我想給: 它只是似乎並沒有要我用同樣的VAR方法中如說明書中,但該方法怎麼會知道要返回什麼呢?

//adds together any other charges from partsLaborGroupBox this is an output method 
private void OtherCharges(TextBox partsTextBox,TextBox laborTextBox, out decimal laborCost, out decimal partsCost) 
{ 
    decimal laborCost = 0m; 
    decimal partsCost = 0m; 


    //this chain goes through the partsLaborTextBox and checks to see if there is input, if so it gets the input 
    //if input is not valid it will display a message. 
    if (!string.IsNullOrEmpty(partsTextBox.Text)) 
    { 
     if(decimal.TryParse(partsTextBox.Text, out partsCost)) 
     { 
      //do nothing 
     } 
     else 
     { 
      MessageBox.Show("Invalid input for parts"); 
     } 
    } 
    if (!string.IsNullOrEmpty(laborTextBox.Text)) 
    { 
     if(decimal.TryParse(laborTextBox.Text, out laborCost)) 
     { 
      //dont need to do anything. 
     } 
     else 
     { 
      MessageBox.Show("Invalid input for labor"); 
     } 
    } 
} 
+3

不要再聲明變量 - 它已經聲明爲參數。只是刪除聲明 - 你傳遞爲'out'這意味着修改參數值將修改您傳遞給方法調用的引用 – Charleh

+0

撇開重複的局部變量/參數名稱,我建議不要有多個返回值這個。只返回一個值是一個好習慣。因此,如果您必須返回兩個值,請將它們包裝在Tuple 中並返回它們,而不是使用輸出參數。 –

+1

是的,我看到了Tuple的一些提及,但我還沒有得到那麼多。這是針對一個班級的,而且我正在編寫章節末尾的小編程問題。整個程序並不是我寫這個程序的方式,但這一章是關於方法的,所以我認爲他們只是試圖讓我們儘可能地使用方法,並根據我們已經知道的方法找出解決方案。就我個人而言,我不會讓該方法返回兩個值,但這就是他們希望我做的。 – BrandenS

回答

3

在方法的頂部取下變量聲明(它們是在方法定義中聲明):

laborCost = 0m; 
partsCost = 0m; 

你基本上是用聲明的範圍相同名稱的新變量。如果你寫會出現同樣的錯誤:

int i = 10; 
int i = 40; 
+0

哦,是的,那很簡單。非常感謝你的幫助和如此瘋狂的快速。你們都很棒。 – BrandenS

1

你可以有多個out參數,但你不能聲明具有相同的名稱作爲另一個是範圍內的變量。

因此,如果您有一個名爲laborCost的參數的方法,則不能同時定義具有該名稱的變量,因爲這會發生衝突。所以給你的變量或你的參數另一個名字,你會沒事的。

0

您的參數和本地值具有相同的名稱。

使用下面的代碼,

//adds together any other charges from partsLaborGroupBox this is an output method 
    private void OtherCharges(TextBox partsTextBox,TextBox laborTextBox, out decimal laborCost, out decimal partsCost) 
    { 
     laborCost = 0m; 
     partsCost = 0m; 


     //this chain goes through the partsLaborTextBox and checks to see if there is input, if so it gets the input 
     //if input is not valid it will display a message. 
     if (!string.IsNullOrEmpty(partsTextBox.Text)) 
     { 
      if(decimal.TryParse(partsTextBox.Text, out partsCost)) 
      { 
       //do nothing 
      } 
      else 
      { 
       MessageBox.Show("Invalid input for parts"); 
      } 
     } 
     if (!string.IsNullOrEmpty(laborTextBox.Text)) 
     { 
      if(decimal.TryParse(laborTextBox.Text, out laborCost)) 
      { 
       //dont need to do anything. 
      } 
      else 
      { 
       MessageBox.Show("Invalid input for labor"); 
      } 
     } 

    } 
0

您需要在參數列表或withon的方法要麼改變laborCost聲明不同的名稱,或者您需要完全去除declaratioin部分,使之形成分配,

喜歡的東西

laborCost = 0m; 
partsCost = 0m; 

withoout前述小數類型聲明。

1

刪除方法內的變量聲明。意思是,從你的代碼中刪除下面;

decimal laborCost = 0m; 
decimal partsCost = 0m; 

您不能聲明與已經聲明爲方法參數的名稱相同的變量。不過,您可以按如下方式分配值。

laborCost = 0m; 
partsCost = 0m; 

此外,我認爲這將是方便的,如果你改變TryParse如下;因爲當(decimal.TryParse)的結果爲真時,你不會做任何事情。

if (!string.IsNullOrEmpty(partsTextBox.Text)) 
    { 
     if(!decimal.TryParse(partsTextBox.Text, out partsCost)) 
     { 
      MessageBox.Show("Invalid input for parts"); 
     } 
    }