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;
    }
4

6 回答 6

9

在外部定义它while

string cust_num = null;
while ...

然后在 while 内设置如下:

cust_num = Console.ReadLine();

因为您在一段时间尝试访问它:

return cust_num;
于 2013-08-14T13:52:04.193 回答
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; 
于 2013-08-14T14:03:27.227 回答
1

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

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

于 2013-08-14T13:52:52.507 回答
1

您试图返回定义它的块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;
}
于 2013-08-14T13:54:04.373 回答
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
于 2013-08-14T13:56:12.283 回答
0

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

有关详细信息,请参阅此链接:http: //msdn.microsoft.com/en-us/library/ms973875.aspx

于 2013-08-14T13:55:10.170 回答