Variable Does Not Exist in the Current Context

Variable does not exist in the current context?

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;

Variable does not exist in current context

You've closed off your method and left out the if statement! The random1 variable is defined and declared within your method so it doesn't exist outside of it. Please move the method's closing bracket to include the if statement as well.

Also, your two if statements should really be linked together with an else if. You've declared two separate if statements so only one of them will have the else. Not wrong, just better practice to the following.

Basic structure:

public void Button_Click(object sender, RoutedEventArgs e) {

...
int random1

if(<random1) {
random1
} else if(>random1) {
...
} else {
...
}

} // <- method closing bracket

Edit: Since you've heavily modified the code provided I'll have to update my explanation.

Your issue has to do with variable scope. A variable defined within a method has local scope to that method. It's not accessible and doesn't even exist outside of it. You should be declaring your method OUTSIDE all the methods so that you can have multiple methods using it.

Basic structure:

int random1

public void methodA() {
random1 = whatever
}

public void methodB() {
if(random1) {
...
}
}

Variable does not exist in current context

When you insert HTML elements into your Razor file, it escapes out of C# mode, so you need to add the @ sign to your variable calls.

EDIT: You also need the @ sign on the while and declaration statements:

@{ int i = 0; }

@while(list[i] != null)
{
<dl class="dl-horizontal">
<dt>
@list[i].Name
</dt>

<dd>
@list[i].Damage
</dd>
</dl>
i++;
}

This will put you back into the C# context and evaluate the expression.

Variable does not exist in the current context while debugging

It's possible the local variables have been optimised away by the JIT compiler. Since you're using Visual Studio you might be able to switch the configuration to Debug and rebuild.

If not, you can configure the JIT compiler to disable optimisations and generate tracking information - see here on how to set the configuration. This should allow you to see local variable when you attach the debugger to the process.



Related Topics



Leave a reply



Submit