Scope issue in function, local variable not defined [duplicate]
0
1
This question already has an answer here:
C#: Use of unassigned local variable, using a foreach and if
5 answers
I am getting an error for dateClose.closing, "use is unassigned local variable". I declared dateClose outside of the for loop and defined the value inside the for loop. How can I make that value available outside of the for loop? public class SMA { public Models.DateClose SMAMethod (Queue<Models.DateClose> queue, int period) { decimal average, sum=0; Models.DateClose dateClose; for (int i = 0; i < period; i++) { dateClose = queue.Dequeue(); sum += dateClose.Close; } average = sum/period; ...