2015-02-10 283 views
-3

試圖檢查我一直在做的這個程序,我似乎打了另一個回來的錯誤說:在控制離開當前方法之前,out參數'checkedIfInsured'必須分配給。輸出參數問題

如果需要,我可以粘貼剩餘的代碼,但對於我來說,看起來很好。

static void GetData(out int patientsID, out string patientsName, out int patientsAge, out decimal patientsAmount, object o, out char checkedIfInsured) 
    { 
     string inString; 
     int count = 3; 
     char test; 
     Console.Write("Please enter Patients ID number>> "); 
     inString = Console.ReadLine(); 
     int.TryParse(inString, out patientsID); 
     Console.Write("Please Enter Name for " + "Patient {0} >> ", patientsID); 
     patientsName = Console.ReadLine(); 
     Console.Write("Please Enter The Age For " + "Patient {0}>> ", patientsName); 
     inString = Console.ReadLine(); 
     int.TryParse(inString, out patientsAge); 
     Console.Write("Please Enter The Amount Due For " + "Patient {0}>> ", patientsID); 
     inString = Console.ReadLine(); 
     decimal.TryParse(inString, out patientsAmount); 
     Console.WriteLine("-----------------------------------"); 

     if (o is InsuredPatient) 
     { 
      Console.WriteLine(" Enter the name of the Patients Insurance Company Code>>"); 
       for (int x = 0; x < count; ++x) 
        Console.WriteLine("{0,-3} = {1,5}", InsuredPatient.InsurerCharacter[x], InsuredPatient.InsurerName[x]); 
      Console.WriteLine(" Enter talent code >> "); 
      test = Console.ReadKey().KeyChar; 

      for (int i = 0; i < InsuredPatient.InsurerCharacter[i]; ++i) 
       if (test == InsuredPatient.InsurerCharacter[i]) 
       { 
        checkedIfInsured = InsuredPatient.InsurerCharacter[i]; 
       } 
     } 


    } 
+0

錯誤的含義正是它所說的。如果循環中的if語句不是真的(或者如果'InsuredPatient.InsurerCharacter [i]'是'<= 0','o是InsuredPatient'就是false),那麼'checkIfInsured'永遠不會被分配。 – 2015-02-10 21:46:46

+1

這個函數是一個重構的canadate,它被重構爲靜態GetDataResults GetData(object o),並將所有這些out語句返回到一個自定義返回類型中。除非你是P /調用非託管代碼,否則在同一時間用'void'返回類型和'out'參數執行一個函數是很好的設計選擇。 – 2015-02-10 21:48:17

+1

順便說一句,使用那麼多'out'參數似乎是非常糟糕的設計。只需返回一個包含相關值的對象。 – 2015-02-10 21:48:47

回答

0

如果if不正確,您仍然需要在else中分配一個值。代碼的所有分支必須返回一個值。

if (o is InsuredPatient) 
    {//...} 
    else{ 
     //default to whatever. 
     checkedIfInsured = myDefaultInsuredCheckedValue; 
    } 
0

您只在'if'塊內分配'checkedIfInsured'。如果條件不成立,它將不會被分配,這就是編譯器所抱怨的。

確保您在'if'塊之外分配'checkedIfInsured'。

+0

大規模downvoter,照顧評論? – CesarGon 2015-02-10 21:50:01

0

如果您使用的是o is InsuredPatient,則只能分配checkIfInsured參數。編譯器告訴你它需要總是被分配給。

0

您只給checkedIfInsured一個特定的條件分支的值。編譯器告訴你,在方法結束之前,您必須必須給它一個值,而不管它需要什麼條件分支。

您可以通過在方法開始時將checkedIfInsured設置爲默認值來避免此錯誤。

+1

有趣。你是否因爲答案錯誤,或者因爲問題不是很好而投票不起來?我的意思是,這不是,但仍然... – 2015-02-10 21:50:08

+2

我低估了,因爲你正在回答這個問題,積極餵養幫助吸血鬼。用谷歌搜索發佈的錯誤消息給出了與第一個結果完全相同的副本,實際上*由在此處回答的用戶回答*。這是一個明顯的重複,對於找到他們是否搜索五秒鐘的人來說,這不是一件困難的事情。 – 2015-02-10 21:52:31

+0

@eddie_cat:看起來很刺耳。不過,如果你感覺強烈,你可以投票結束。這是我做的。我目前似乎是唯一一個。 – 2015-02-10 21:54:13

0

checkedIfInsured可能並不總是有一個值,因爲它在一個if塊內。如果不符合if的標準怎麼辦?

0

編譯器抱怨,因爲存在未設置checkedIfInsured的代碼路徑,即test變量不等於InsuredPatient.InsurerCharacter[i]的情況。

您可以做的是將checkedIfInsured設置爲方法開始處的某個默認字符,以處理test不等於InsuredPatient.InsurerCharacter[i]的情況。