Variable does not exist in the current context?

2019-01-04 04:07发布

I know this is most likely a stupid question but I am a university student who is new to C# and object-oriented programming. I have tried to find the answer elsewhere but I wasn't able to find anything that could help. The debugger keeps telling me that the variable 'cust_num does not exist in the current context'. If someone can tell me what I have done wrong and make me feel like an idiot, I would greatly appreciate it. Thanks!

    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
6条回答
The star\"
2楼-- · 2019-01-04 04:19

Your return cust_num statement is outside of the definition context of cust_num. Since you defined it inside your while loop, it exists only in that scope. You need to move it out of the loop.

Any local variable you define exists only within the curly brackets that encapsulate it (and in any nested brackets).

查看更多
闹够了就滚
3楼-- · 2019-01-04 04:28

Each variable in C# exists within a scope which is defined by curly braces:

{ 
  ...
  int x = 0;
  ...
  x = x + 1; // <- legal
  ...
  // <- x is defined up to here
}

x = x - 1; // <- illegal, providing there's no other "x" declared

In your case, cust_num is restricted by while {...}. It has to think what value should your code return if cust_num_valid = true and there's no cust_num at all.

  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

To repair your code put string cust_num = ""; outside the 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; 
查看更多
\"骚年 ilove
4楼-- · 2019-01-04 04:30

When a variable is defined in a code block, it is restrained to that scope (and of course starts at the variable declaration; you cannot use it before it is declared). If you look in your example, the variable is defined in the while block, but used outside of it.

string get_cust_num()
{
    while ()
    {
        string cust_num = Console.ReadLine(); // cust_num scope starts here
        if ()
        {
        }
    } // cust_num scope ends here
    return cust_num;
}

You need to define it at the method level to use it :

string get_cust_num()
{
    string cust_num = Console.ReadLine(); // cust_num scope starts here

    while ()
    {
        if ()
        {
        }
    }

    return cust_num;
} // cust_num scope ends here
查看更多
别忘想泡老子
5楼-- · 2019-01-04 04:37

Define it outside the while:

string cust_num = null;
while ...

and then inside the while set it like this:

cust_num = Console.ReadLine();

because you're trying to access it after the while:

return cust_num;
查看更多
Fickle 薄情
6楼-- · 2019-01-04 04:40

It appears that you are trying to return the value of cust_num. In order to return the value of cust_num, it needs to be declared outside of the while loop at the same level as where the "return" statement occurs.

See this link for more information: http://msdn.microsoft.com/en-us/library/ms973875.aspx

查看更多
一夜七次
7楼-- · 2019-01-04 04:43

You're trying to return cust_num outside of the scope of the while block where it is defined. You need to define it outside of the while if you wish to return it, for example:

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;
}
查看更多
登录 后发表回答