SKale
SKale

Reputation: 571

The name 'xyz' does not exist in the current context

This is probably really basic in C#, but I looked around a lot to find a solution.

In my MVC controller's action method, I have an incoming routeid (programid) that I need to use to create another string variable (accounttype). I have an if/else to evaluate the value of accounttype. Later in the same action method's code, I have nother if/else that takes the variable accounttype and creates a JSON string to pass to a payment gateway. But I get an error "The name 'accounttype' does not exist in current context.' Do I need to declare it as public or something?

Here are the two if/else statements:

if (programid == 0)
{
    string accounttype = "Membership";
}
else
{
    string accounttype = "Program";
}

Later on in same controller action, I need to use the accounttype variable to calculate another string variable (URL)

if (results.Count() > 0)
{
    string URL = accounttype + "some text"
}
else
{
    string URL = accounttype + "some other text"
}

Upvotes: 0

Views: 2179

Answers (7)

Save
Save

Reputation: 11938

Scope is your problem :)

Since I guess you are new, I'll try to define it in simple words: the scope of a variable is the place where a variable lives. Not all the variables can be called everywhere in your program, and we call those who can, global.

In your case, you are declaring those variables inside an if .. else statement, and, because of C# rules, they die as soon as the if block ends. This is what the compiler is telling you: you can't call something that doesn't exist.

To solve your problem, you just have to declare

string accounttype;

before the if.. else and you will be fine.

If you want to read more about scopes, this is a good place to start!

Upvotes: 2

Allister
Allister

Reputation: 61

Because you define the variables within closures, they fall out of scope as soon as you exit those closures. You need:

string URL;
if(test)
{
    URL = "1";
} else {
    URL = "2";
}

Upvotes: 0

Z .
Z .

Reputation: 12837

the scope of the account type variable is in the { } of the if. you need to declare the variable outside the if to be able to use it after, or even better:

string accounttype = programid == 0 ? "Membership" : "Program";

Upvotes: 0

Jeff Hornby
Jeff Hornby

Reputation: 13640

This is a scoping problem. Anything inside of braces {} is defined as a block. Any variables defined in a block are only available within that block and are garbage collected when you exit the block.

Don't define accountType inside the blocks in your if statement:

string accounttype;

if (programid == 0)
{
    accounttype = "Membership";
}
else
{
    accounttype = "Program";
}

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149020

The problem is that you're defining your accounttype variable within the scope of your if and else block, so it's not defined outside of those blocks.

Try declaring your variables outside the if/else blocks:

string accounttype;
string URL;
if (programid == 0)
{
    accounttype = "Membership";
}
else
{
    accounttype = "Program";
}

if (results.Count() > 0)
{
    URL = accounttype + "some text"
}
else
{
    URL = accounttype + "some other text"
}

Or if your code is really this simple, just use the conditional operator:

string accounttype = programid == 0 ? "Membership" : "Program";
string URL = accounttype + (results.Any() ? "some text" : "some other text");

Upvotes: 1

Murdock
Murdock

Reputation: 4662

The scope of the accounttype is limited to the if statement. Do

string accounttype;

if (programid == 0)
{
 accounttype = "Membership";
}
else
{
 accounttype = "Program";
}

Upvotes: 1

Mister Epic
Mister Epic

Reputation: 16723

Your scoping is incorrect, try this:

string accounttype;
if (programid == 0)
{
    accounttype = "Membership";
}
else
{
    accounttype = "Program";
}

Upvotes: 0

Related Questions