变量不会在目前的情况下是否存在? [英] Variable does not exist in the current context?
问题描述
我知道这是最有可能是愚蠢的问题,但我是一个大学生谁是新的C#和面向对象编程。我试图在别处找到了答案,但我没能找到任何可能的帮助。调试器不断告诉我该变量CUST_NUM不会在目前的情况下存在。如果有人能告诉我什么,我做错了,让我觉得自己像个白痴,我将不胜感激。 !谢谢
字符串get_cust_num()
{
布尔cust_num_valid = FALSE; (!cust_num_valid)
在
{
cust_num_valid = TRUE;
Console.Write(请输入客户编号:);
串CUST_NUM =到Console.ReadLine();
如果(CUST_NUM ==000000||!Regex.IsMatch(CUST_NUM,@^ [0-9] + $)|| cust_num.Length!= 6)
{
cust_num_valid = FALSE;
Console.WriteLine(无效的客户编号检测到的顾客数必须是6位正整数(零无效));
}
}
返回CUST_NUM;
}
在C#中每个变量是存在内的范围的是由大括号定义的:
{
...
INT X = 0;
...
X = X + 1; //< - 法律
...
//< - x被定义到这里
}
X = X - 1; //< - 非法的,提供没有其他的X,宣布
在你的情况, CUST_NUM
是限制而{...}
。它想什么价值应该代码的回报,如果的 cust_num_valid = TRUE 的,有没有
的 CUST_NUM 的都没有。
而
{//<(cust_num_valid!); - CUST_NUM范围开始
cust_num_valid = TRUE;
Console.Write(请输入客户编号:);
串CUST_NUM =到Console.ReadLine();
如果(CUST_NUM ==000000||!Regex.IsMatch(CUST_NUM,@^ [0-9] + $)|| cust_num.Length!= 6)
{
cust_num_valid = FALSE;
Console.WriteLine(无效的客户编号检测到的顾客数必须是6位正整数(零无效));
}
} //< - CUST_NUM范围结束
返回CUST_NUM; //< - 出范围
的
要修复你的代码放在字符串CUST_NUM =;
的 的外,而
:
字符串CUST_NUM =; //< - (!cust_num_valid)声明
在
{
cust_num_valid = TRUE;
Console.Write(请输入客户编号:);
CUST_NUM =到Console.ReadLine(); //< - 没有新的声明:串被删除
如果(CUST_NUM ==000000|| Regex.IsMatch(CUST_NUM,@^ [0-9] + $ !)|| cust_num.Length = 6)
{
cust_num_valid = FALSE;
Console.WriteLine(无效的客户编号检测到的顾客数必须是6位正整数(零无效));
}
}
返回CUST_NUM;
I know this is most likely a stupid question but I am a university student who is new to C# and object-oriented programming. I have tried to find the answer elsewhere but I wasn't able to find anything that could help. The debugger keeps telling me that the variable 'cust_num does not exist in the current context'. If someone can tell me what I have done wrong and make me feel like an idiot, I would greatly appreciate it. Thanks!
string get_cust_num()
{
bool cust_num_valid = false;
while (!cust_num_valid)
{
cust_num_valid = true;
Console.Write("Please enter customer number: ");
string cust_num = Console.ReadLine();
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
}
return cust_num;
}
Each variable in C# is exists within a scope which is defined by curly braces:
{
...
int x = 0;
...
x = x + 1; // <- legal
...
// <- x is defined up to here
}
x = x - 1; // <- illegal, providing there's no other "x" declared
In your case, cust_num
is restricted by while {...}
. It has to think what value should your code return if cust_num_valid = true and there's no
cust_num at all.
while (!cust_num_valid)
{ // <- Scope of cust_num begins
cust_num_valid = true;
Console.Write("Please enter customer number: ");
string cust_num = Console.ReadLine();
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
} // <- Scope of cust_num ends
return cust_num; // <- out of scope
To repair your code put string cust_num = "";
outside the while
:
string cust_num = ""; // <- declaration
while (!cust_num_valid)
{
cust_num_valid = true;
Console.Write("Please enter customer number: ");
cust_num = Console.ReadLine(); // <- no new declaration: "string" is removed
if (cust_num == "000000" || !Regex.IsMatch(cust_num, @"^[0-9]+$") || cust_num.Length != 6)
{
cust_num_valid = false;
Console.WriteLine("Invalid customer number detected. Customer numbers must be a 6 digit positive integer (zeros will not work)");
}
}
return cust_num;
这篇关于变量不会在目前的情况下是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!