当前上下文中不存在该变量?

7

我知道这可能是一个愚蠢的问题,但我是一名刚开始学习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;
    }
6个回答

9

将其定义在while之外:

string cust_num = null;
while ...

然后在 while 循环中设置如下内容:
cust_num = Console.ReadLine();

因为你尝试在 while 后访问它:

return cust_num;

作为一种替代方案:您可以在循环外声明 string cust_num;(不定义 = null),并使用 do { } while() 循环(因为该循环至少会执行一次)。 - Nolonar

7

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; 

1
你试图在定义 cust_numwhile 块之外返回它。如果你想要返回它,需要在 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;
}

1

当变量在代码块中定义时,它被限制在该作用域内(当然,从变量声明开始;你不能在声明之前使用它)。如果你看一下你的例子,变量是在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

1
你的return cust_num语句在cust_num的定义上下文之外。由于你将它定义在while循环内部,因此它只存在于该范围内。你需要将它移出循环。
任何你定义的局部变量只存在于封装它的花括号(以及任何嵌套的括号)中。

0

不要忘记,如果您在发布后意识到已经有另一个更好或相同的答案被发布,删除您的帖子以避免在问题中添加没有任何贡献的答案是没有成本的。 - Michelle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接