C# 排除参数问题
去检查我一直在做的这个程序,我似乎遇到了另一个错误,说回来;在控件离开当前方法之前,必须将out参数“checkedFinSured”指定给 如果有必要,我可以粘贴代码的其余部分,但对我来说,它看起来不错C# 排除参数问题,c#,C#,去检查我一直在做的这个程序,我似乎遇到了另一个错误,说回来;在控件离开当前方法之前,必须将out参数“checkedFinSured”指定给 如果有必要,我可以粘贴代码的其余部分,但对我来说,它看起来不错 static void GetData(out int patientsID, out string patientsName, out int patientsAge, out decimal patientsAmount, object o, out char checkedIfInsur
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];
}
}
}
static void GetData(out int patientsID,out string patientsName,out int patientsAge,out decimal patientsAmount,object o,out char checkedFinSured)
{
弦安装;
整数计数=3;
煤焦试验;
控制台。写入(“请输入患者ID号>>”;
inString=Console.ReadLine();
内锥虫(内锥虫,外锥虫);
编写(“请输入“+”患者{0}>>”的名称,patientsID);
patientsName=Console.ReadLine();
Console.Write(“请输入“+”患者{0}>>”的年龄,patientsName);
inString=Console.ReadLine();
内锥虫(内锥虫、外锥虫);
控制台。写入(“请输入“+”患者{0}>>”的应付金额,patientsID);
inString=Console.ReadLine();
十进制.锥巴色(装入,取出);
Console.WriteLine(“-----------------------------------------”;
如果(o为被保险患者)
{
Console.WriteLine(“输入患者保险公司代码>>的名称”);
对于(整数x=0;x>”;
test=Console.ReadKey().KeyChar;
for(int i=0;i
您仅在“if”块内分配“checkedFinSured”。如果条件不成立,它将不会被赋值,这就是编译器所抱怨的
确保在“if”块之外指定“checkedFinSured”。如果
o已投保患者,则仅指定checkedInsurared
参数。编译器告诉您需要始终将其赋值。您只在一个特定的条件分支中给checkedFinSured
一个值。编译器告诉您必须在方法结束之前给它一个值,不管它采用什么条件分支
您可以通过在方法开始时将checkedFinSured
设置为默认值来避免此错误。如果如果不正确,您仍然需要在中指定一个值。代码的所有分支都必须返回一个值
if (o is InsuredPatient)
{//...}
else{
//default to whatever.
checkedIfInsured = myDefaultInsuredCheckedValue;
}
CheckeFinSured可能并不总是有值,因为它位于if块内。如果不满足if的标准怎么办?编译器正在抱怨,因为存在一个未设置checkedFinSured
的代码路径,即test
变量不等于InsuredPatient.InsurerCharacter[i]
您可以做的是在方法的开头将checkedFinSured
设置为某个默认字符,以处理test
不等于InsuredPatient.InsurerCharacter[i]
的情况。错误的意思与它所说的完全相同。如果循环中的If
子句从来都不是真的(或者InsuredPatient.InsurerCharacter[i]
是此函数是一个主要的canadate,需要重构为静态GetDataResults GetData(对象o)
并在一个自定义返回类型中返回所有这些out语句。除非您是P/调用非托管代码,否则同时使用void
返回类型和out
参数执行函数是一个很好的设计选择。顺便说一句,使用这么多out
参数看起来非常糟糕签名。只需返回一个包含相关值的对象。大量downvoter,是否要评论?有趣。你是因为答案错误而拒绝投票,还是因为问题不好?我的意思是,不是,但仍然…我拒绝投票,因为你通过回答这个问题来积极地喂养帮助吸血鬼。用谷歌搜索发布的错误消息作为第一个结果给出了一个精确的副本,这实际上是由一个在这里回答的用户回答的。这是一个明显的副本,即使对于搜索了5秒钟的人来说也不难找到。@eddie_cat:似乎很苛刻。但是,如果你感觉强烈,你可以投票关闭。这就是我所做的。我现在似乎不太喜欢他只有一个。@MattBurland我把它标为复制品,没有投票权关闭鲍尔斯。@eddie_猫我尊重你否决投票的决定-你是对的。投票关闭。