c# - The name 'xyz' does not exist in the current context -
this basic in c#, looked around lot find solution.
in mvc controller's action method, have incoming routeid (programid) need use create string variable (accounttype). have if/else evaluate value of accounttype. later in same action method's code, have nother if/else takes variable accounttype , creates json string pass payment gateway. error "the name 'accounttype' not exist in current context.' need declare public or something?
here 2 if/else statements:
if (programid == 0) { string accounttype = "membership"; } else { string accounttype = "program"; } later on in same controller action, need use accounttype variable calculate string variable (url)
if (results.count() > 0) { string url = accounttype + "some text" } else { string url = accounttype + "some other text" }
the problem you're defining accounttype variable within scope of if , else block, it's not defined outside of blocks.
try declaring variables outside 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 code simple, use conditional operator:
string accounttype = programid == 0 ? "membership" : "program"; string url = accounttype + (results.any() ? "some text" : "some other text");
Comments
Post a Comment