试图检查我一直在做的这个程序,我似乎打了另一个回来的错误说:在控制离开当前方法之前,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