【问题标题】:Out Parameters Questions [duplicate]输出参数问题[重复]
【发布时间】:2015-02-10 21:43:55
【问题描述】:

去检查我一直在做的这个程序,我似乎又回到了另一条路,错误说;在控制离开当前方法之前,必须分配 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]&lt;= 0o is InsuredPatient 为假),则永远不会分配checkIfInsured
  • 这个函数是一个主要的候选函数,可以重构为static GetDataResults GetData(object o),并以单个自定义返回类型返回所有​​这些 out 语句。除非您在非托管代码中 P/Invoking,否则很少会同时执行具有 void 返回类型和 out 参数的函数是一个很好的设计选择。
  • 顺便说一句,使用这么多out 参数似乎是非常糟糕的设计。只需返回一个包含相关值的对象。

标签: c#


【解决方案1】:

如果if 不正确,您仍然需要在else 中分配一个值。代码的所有分支都必须返回一个值。

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

【讨论】:

    【解决方案2】:

    如果o is InsuredPatient,您只分配checkIfInsured 参数。编译器告诉你它需要总是被赋值。

    【讨论】:

      【解决方案3】:

      你只在一个特定的条件分支中给checkedIfInsured 一个值。编译器告诉您,您必须在方法结束之前给它一个值,无论它采用什么条件分支。

      您可以通过在方法开始时将checkedIfInsured 设置为默认值来避免此错误。

      【讨论】:

      • 有趣。你投反对票是因为答案是错误的,还是因为问题不好?我的意思是,它不是,但仍然......
      • 我投了反对票,因为您通过回答这个问题正在积极地喂养一个帮助吸血鬼。谷歌搜索发布的错误消息会得到与第一个结果完全相同的结果,该结果实际上是由此处回答的用户回答。这是一个明显的重复,即使搜索 5 秒钟也不会很难找到。
      • @eddie_cat:看起来很苛刻。不过,如果您感觉强烈,您可以投票关闭。这就是我所做的。我目前似乎是唯一的一个。
      • @MattBurland 我将其标记为重复,没有投票关闭权力。
      • @eddie_cat 我尊重你投反对票的决定——你是对的。投票结束。
      【解决方案4】:

      checkedIfInsured 可能并不总是有一个值,因为它位于 if 块中。如果不满足if条件怎么办?

      【讨论】:

        【解决方案5】:

        编译器抱怨是因为存在一个未设置checkedIfInsured 的代码路径,即test 变量不等于InsuredPatient.InsurerCharacter[i] 的情况。

        您可以在方法的开头将checkedIfInsured 设置为某个默认字符,以处理test 不等于InsuredPatient.InsurerCharacter[i] 的情况。

        【讨论】:

          【解决方案6】:

          您只是在“if”块内分配“checkedIfInsured”。如果条件不成立,它将不会被分配,这就是编译器所抱怨的。

          确保在“if”块之外分配“checkedIfInsured”。

          【讨论】:

          • 大量反对者,愿意发表评论吗?
          猜你喜欢
          • 2015-08-06
          • 1970-01-01
          • 1970-01-01
          • 2021-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-19
          • 1970-01-01
          相关资源
          最近更新 更多