試圖檢查我一直在做的這個程序,我似乎打了另一個回來的錯誤說:在控制離開當前方法之前,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];
}
}
}
錯誤的含義正是它所說的。如果循環中的if語句不是真的(或者如果'InsuredPatient.InsurerCharacter [i]'是'<= 0','o是InsuredPatient'就是false),那麼'checkIfInsured'永遠不會被分配。 – 2015-02-10 21:46:46
這個函數是一個重構的canadate,它被重構爲靜態GetDataResults GetData(object o),並將所有這些out語句返回到一個自定義返回類型中。除非你是P /調用非託管代碼,否則在同一時間用'void'返回類型和'out'參數執行一個函數是很好的設計選擇。 – 2015-02-10 21:48:17
順便說一句,使用那麼多'out'參數似乎是非常糟糕的設計。只需返回一個包含相關值的對象。 – 2015-02-10 21:48:47