当前上下文中不存在变量

本文关键字:不存在 变量 上下文 | 更新日期: 2023-09-27 18:27:50

我知道这很可能是一个愚蠢的问题,但我是一名刚接触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;
    }

当前上下文中不存在变量

while:之外定义它

string cust_num = null;
while ...

然后在里面设置成这样:

cust_num = Console.ReadLine();

因为您正试图在之后访问,而

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_numwhile {...}的限制。它必须考虑如果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

要修复代码,请将string cust_num = "";放在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; 

您的return cust_num语句在cust_num的定义上下文之外。由于您在while循环中定义了它,所以它只存在于该范围中。你需要把它移出循环。

您定义的任何局部变量都只存在于封装它的花括号中(以及任何嵌套的括号中)。

您试图在定义while块的范围之外返回cust_num。如果您希望返回它,您需要在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