【问题标题】:Variable does not exist in the current context?当前上下文中不存在变量?
【发布时间】:2023-03-24 04:57:01
【问题描述】:

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


    【解决方案1】:

    while之外定义它:

    string cust_num = null;
    while ...
    

    然后在while里面这样设置:

    cust_num = Console.ReadLine();
    

    因为您试图在 while 之后访问它:

    return cust_num;
    

    【讨论】:

    • 作为替代方案:您可以在循环外声明string cust_num;(不定义= null)并使用do { } while() 循环(因为循环将始终至少执行一次)。
    【解决方案2】:

    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; 
    

    【讨论】:

      【解决方案3】:

      您的return cust_num 语句超出了cust_num 的定义上下文。由于您在 while 循环中定义了它,因此它仅存在于该范围内。您需要将其移出循环。

      您定义的任何局部变量仅存在于封装它的大括号内(以及任何嵌套的括号内)。

      【讨论】:

        【解决方案4】:

        您试图在定义它的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;
        }
        

        【讨论】:

          【解决方案5】:

          当一个变量在代码块中定义时,它被限制在那个范围内(当然从变量声明开始;在声明之前你不能使用它)。如果您查看示例,则该变量是在 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
          

          【讨论】:

            【解决方案6】:

            您似乎正试图返回cust_num 的值。为了返回cust_num 的值,它需要在while 循环之外与“return”语句发生的同一级别声明。

            查看此链接了解更多信息:http://msdn.microsoft.com/en-us/library/ms973875.aspx

            【讨论】:

            • 不要忘记,如果您在发布后意识到已经发布了另一个答案(更好或相同),则删除您的帖子是免费的,以避免与不正确的答案混淆问题'不要添加任何东西。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多