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

后端 未结 7 1833
广开言路
广开言路 2020-12-11 13:45

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

7条回答
  •  一向
    一向 (楼主)
    2020-12-11 14:02

    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");
    

提交回复
热议问题