Does the variable not exist in the current context?

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 tried to find the answer elsewhere, but I could not find anything that could help. The debugger continues to tell me that the variable 'cust_num does not exist in the current context'. If someone can tell me what I did wrong and make me feel like an idiot, I would really 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; } 
+6
source share
6 answers

Each variable in C # exists within the scope, which is determined by curly braces:

 { ... int x = 0; ... x = x + 1; // <- legal ... // <- x is defined up to here } x = x - 1; // <- illegal, providing there no other "x" declared 

In your case, cust_num limited to while {...} . He should think about what value your code should return if cust_num_valid = true and there is 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 restore the code, put string cust_num = ""; outside 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; 
+4
source

Define it outside while :

 string cust_num = null; while ... 

and then inside while set it like this:

 cust_num = Console.ReadLine(); 

because you are trying to access it after :

 return cust_num; 
+9
source

Your return cust_num is outside the context of the cust_num definition. Since you defined it inside a while , it exists only in this area. You need to get it out of the loop.

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

+1
source

You are trying to return cust_num outside the while scope where it is defined. You must define it out of time if you want 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; } 
+1
source

When a variable is defined in a code block, it is constrained by this area (and, of course, begins with a variable declaration; you cannot use it before its declaration). If you look in your example, the variable is defined in the while block, but is 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 in order 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 
+1
source

It looks like you are trying to return cust_num . To return the value of cust_num , it must be declared outside the while loop at the same level as when the return statement is executed.

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

0
source

Source: https://habr.com/ru/post/951724/


All Articles