变量在当前上下文中不存在?
我知道这很可能是一个愚蠢的问题,但我是一名大学生,他是C#和面向对象编程的新手。 我试图在别处找到答案,但我找不到任何有用的东西。 调试器一直告诉我变量’cust_num在当前上下文中不存在’。 如果有人能告诉我我做错了什么并让我觉得自己像个白痴,我会非常感激。 谢谢!
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; }
C#中的每个变量都存在于由花括号定义的范围内 :
{ ... int x = 0; ... x = x + 1; // <- legal ... // <- x is defined up to here } x = x - 1; // <- illegal, providing there's no other "x" declared
在您的情况下, cust_num
受while {...}
限制。 如果cust_num_valid = true并且根本没有cust_num ,它必须考虑代码应该返回什么值。
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
修复你的代码put string cust_num = "";
在外面 :
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;
在while
之外定义它:
string cust_num = null; while ...
然后在里面设置它像这样:
cust_num = Console.ReadLine();
因为你试图在while 之后访问它:
return cust_num;
您的return cust_num
语句位于return cust_num
的定义上下文cust_num
。 由于您在while
循环中定义了它,因此它仅存在于该范围内。 你需要把它移出循环。
您定义的任何局部变量仅存在于封装它的大括号内(以及任何嵌套括号中)。
您试图将cust_num
返回到定义它的while
块的范围之外。 如果要返回它,则需要在while之外定义它,例如:
string get_cust_num() { bool cust_num_valid = false; string cust_num = string.Empty; while (!cust_num_valid) { cust_num_valid = true; Console.Write("Please enter customer number: "); 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; }
当在代码块中定义变量时,它被限制在该范围内(当然从变量声明开始;在声明之前不能使用它)。 如果查看示例,变量在while块中定义,但在其外部使用。
string get_cust_num() { while () { string cust_num = Console.ReadLine(); // cust_num scope starts here if () { } } // cust_num scope ends here return cust_num; }
您需要在方法级别定义它以使用它:
string get_cust_num() { string cust_num = Console.ReadLine(); // cust_num scope starts here while () { if () { } } return cust_num; } // cust_num scope ends here
您似乎正在尝试返回cust_num
的值。 为了返回cust_num
的值,需要在while循环之外声明与“return”语句发生的位置相同的级别。
有关详细信息,请参阅此链接: http : //msdn.microsoft.com/en-us/library/ms973875.aspx
上述就是C#学习教程:变量在当前上下文中不存在?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1044176.html