Use of unassigned local variable that is assigned

情到浓时终转凉″ 提交于 2019-12-04 05:24:54

问题


The below code runs a 'for' loop to create months 1 through 12 then names each month Jan through Dec according to their number. That pieces compiles fine. At the bottom where I try to write the month name on the screen is where it is failing. It says "use of unassigned local variable 'monthName'; however monthName was just used previously and is declared above. Any help you could provide would be greatly appreciated.

for (int month = 1; month <= 12; month++)
{
    string monthName;
    double monthlyProd = .1 * dProdRate;
    double monthlySales = .07 * dSalesRate;
    if (month == 1) { monthName = "Jan"; }
    if (month == 2) { monthName = "Feb"; monthlyProd = 0; }
    if (month == 3) { monthName = "Mar"; }
    if (month == 4) { monthName = "Apr"; }
    if (month == 5) { monthName = "May"; }
    if (month == 6) { monthName = "Jun"; monthlyProd = 0; }
    if (month == 7) { monthName = "Jul"; }
    if (month == 8) { monthName = "Aug"; }
    if (month == 9) { monthName = "Sep"; monthlySales = (.15 * dSalesRate); }
    if (month == 10) { monthName = "Oct"; }
    if (month == 11) { monthName = "Nov"; }
    if (month == 22) { monthName = "Dec"; monthlySales = (.15 * dSalesRate); }
}
dEndingInventory += dPreviousProd - dPreviousSales;
Console.WriteLine("{0}{1,15}{2,15}{3,15}", monthName, monthlyProd, monthlySales, dEndingInventory);

回答1:


You know that month can only take the values 1 to 12 inclusive but the compiler is not that smart. If say month is 0 then the variable monthName is never assigned a value and that is what the compiler is complaining about. To fix it simply initialize the variable when you declare it:

string monthName = null;

Also, there is something fishy about your code because monthName is used outside the loop where it is declared but I assume that this is a typo because the code as it stands now will not give you the error you are asking about.




回答2:


You have declared the monthName, monthlyProd,monthlySales under the scope of For Loop and trying to use those variables out of the scope of For Loop. You should declare variables before for loop-

string monthName;
double monthlyProd = .1 * dProdRate;
double monthlySales = .07 * dSalesRate;

for (int month = 1; month <= 12; month++)
{

    if (month == 1) { monthName = "Jan"; }
    if (month == 2) { monthName = "Feb"; monthlyProd = 0; }
    if (month == 3) { monthName = "Mar"; }
    if (month == 4) { monthName = "Apr"; }
    if (month == 5) { monthName = "May"; }
    if (month == 6) { monthName = "Jun"; monthlyProd = 0; }
    if (month == 7) { monthName = "Jul"; }
    if (month == 8) { monthName = "Aug"; }
    if (month == 9) { monthName = "Sep"; monthlySales = (.15 * dSalesRate); }
    if (month == 10) { monthName = "Oct"; }
    if (month == 11) { monthName = "Nov"; }
    if (month == 22) { monthName = "Dec"; monthlySales = (.15 * dSalesRate); }
}
dEndingInventory += dPreviousProd - dPreviousSales;
Console.WriteLine("{0}{1,15}{2,15}{3,15}", monthName, monthlyProd, monthlySales, dEndingInventory);



回答3:


You will be surely declaring monthName outside scope also(in your loop it declared again). other wise the program should tell compile error. right click on the variable then "Go to defenision". If so variables are different. so only its misbehaving

The reason is two variables having different memory in primary memory. so one memory update won't effect the other. Compiler will use maximum local variable for its processing. Inside loop its the declared one inside. Outside the loop its the outside one

So if you needn't the outer variable value further don't declare it again inside. other wise you declare it outside of the loop with some other name



来源:https://stackoverflow.com/questions/26880288/use-of-unassigned-local-variable-that-is-assigned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!