2015-02-10 299 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]的情况。