Scope issue in function, local variable not defined [duplicate]












0















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;
dateClose.Close = average; <--- error

return dateClose;

}
}









share|improve this question













marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 23:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • The reason for the error is because the variable hasn't been assigned (i.e. it doesn't have a value assigned to it like 'null' or '1'). The answer by Steve below will fix that error.
    – MikeH
    Nov 12 '18 at 22:52
















0















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;
dateClose.Close = average; <--- error

return dateClose;

}
}









share|improve this question













marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 23:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • The reason for the error is because the variable hasn't been assigned (i.e. it doesn't have a value assigned to it like 'null' or '1'). The answer by Steve below will fix that error.
    – MikeH
    Nov 12 '18 at 22:52














0












0








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;
dateClose.Close = average; <--- error

return dateClose;

}
}









share|improve this question














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;
dateClose.Close = average; <--- error

return dateClose;

}
}




This question already has an answer here:




  • C#: Use of unassigned local variable, using a foreach and if

    5 answers








c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 22:42









Jam66125Jam66125

596




596




marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 23:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 23:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • The reason for the error is because the variable hasn't been assigned (i.e. it doesn't have a value assigned to it like 'null' or '1'). The answer by Steve below will fix that error.
    – MikeH
    Nov 12 '18 at 22:52


















  • The reason for the error is because the variable hasn't been assigned (i.e. it doesn't have a value assigned to it like 'null' or '1'). The answer by Steve below will fix that error.
    – MikeH
    Nov 12 '18 at 22:52
















The reason for the error is because the variable hasn't been assigned (i.e. it doesn't have a value assigned to it like 'null' or '1'). The answer by Steve below will fix that error.
– MikeH
Nov 12 '18 at 22:52




The reason for the error is because the variable hasn't been assigned (i.e. it doesn't have a value assigned to it like 'null' or '1'). The answer by Steve below will fix that error.
– MikeH
Nov 12 '18 at 22:52












2 Answers
2






active

oldest

votes


















2














you can simply fix the error by doing



Models.DateClose dateClose = null;


however you would also want to add a null check to make sure you don't run into null ref exception if queue has no item.






share|improve this answer





















  • Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
    – Jam66125
    Nov 13 '18 at 9:57






  • 1




    @Jam66125 because the line is not guarantee to run (I.E. period = 0)
    – Steve
    Nov 13 '18 at 15:29





















1














You can do this. If your period variable is greater than the queue count than dateClose.Close will throw an exception.



public Models.DateClose SMAMethod (Queue<Models.DateClose> queue, int period)
{
decimal average, sum=0;
Models.DateClose dateClose = null;
for (int i = 0; i < period; i++)
{
dateClose = queue.Dequeue();

if(dateClose != null)
sum += dateClose.Close;
}

average = sum/period;
dateClose.Close = average;

return dateClose;

}


Why do you get this error:



if you have the class, member variables neednot be initialized:



public class Test
{
private int temp; // this is okay.

..
}


However, if you have a local variable, then you need to initialize them:



public void Method()
{
int variabl;

sum += variable; // error.
}


So, local variables need to be initialized but member variables neednt be.






share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    you can simply fix the error by doing



    Models.DateClose dateClose = null;


    however you would also want to add a null check to make sure you don't run into null ref exception if queue has no item.






    share|improve this answer





















    • Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
      – Jam66125
      Nov 13 '18 at 9:57






    • 1




      @Jam66125 because the line is not guarantee to run (I.E. period = 0)
      – Steve
      Nov 13 '18 at 15:29


















    2














    you can simply fix the error by doing



    Models.DateClose dateClose = null;


    however you would also want to add a null check to make sure you don't run into null ref exception if queue has no item.






    share|improve this answer





















    • Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
      – Jam66125
      Nov 13 '18 at 9:57






    • 1




      @Jam66125 because the line is not guarantee to run (I.E. period = 0)
      – Steve
      Nov 13 '18 at 15:29
















    2












    2








    2






    you can simply fix the error by doing



    Models.DateClose dateClose = null;


    however you would also want to add a null check to make sure you don't run into null ref exception if queue has no item.






    share|improve this answer












    you can simply fix the error by doing



    Models.DateClose dateClose = null;


    however you would also want to add a null check to make sure you don't run into null ref exception if queue has no item.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 22:45









    SteveSteve

    7,43361542




    7,43361542












    • Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
      – Jam66125
      Nov 13 '18 at 9:57






    • 1




      @Jam66125 because the line is not guarantee to run (I.E. period = 0)
      – Steve
      Nov 13 '18 at 15:29




















    • Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
      – Jam66125
      Nov 13 '18 at 9:57






    • 1




      @Jam66125 because the line is not guarantee to run (I.E. period = 0)
      – Steve
      Nov 13 '18 at 15:29


















    Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
    – Jam66125
    Nov 13 '18 at 9:57




    Steve, this line in the for loop "dateClose = queue.Dequeue();" assigns a value to dateClose.close. Why is it not known outside the for loop?
    – Jam66125
    Nov 13 '18 at 9:57




    1




    1




    @Jam66125 because the line is not guarantee to run (I.E. period = 0)
    – Steve
    Nov 13 '18 at 15:29






    @Jam66125 because the line is not guarantee to run (I.E. period = 0)
    – Steve
    Nov 13 '18 at 15:29















    1














    You can do this. If your period variable is greater than the queue count than dateClose.Close will throw an exception.



    public Models.DateClose SMAMethod (Queue<Models.DateClose> queue, int period)
    {
    decimal average, sum=0;
    Models.DateClose dateClose = null;
    for (int i = 0; i < period; i++)
    {
    dateClose = queue.Dequeue();

    if(dateClose != null)
    sum += dateClose.Close;
    }

    average = sum/period;
    dateClose.Close = average;

    return dateClose;

    }


    Why do you get this error:



    if you have the class, member variables neednot be initialized:



    public class Test
    {
    private int temp; // this is okay.

    ..
    }


    However, if you have a local variable, then you need to initialize them:



    public void Method()
    {
    int variabl;

    sum += variable; // error.
    }


    So, local variables need to be initialized but member variables neednt be.






    share|improve this answer


























      1














      You can do this. If your period variable is greater than the queue count than dateClose.Close will throw an exception.



      public Models.DateClose SMAMethod (Queue<Models.DateClose> queue, int period)
      {
      decimal average, sum=0;
      Models.DateClose dateClose = null;
      for (int i = 0; i < period; i++)
      {
      dateClose = queue.Dequeue();

      if(dateClose != null)
      sum += dateClose.Close;
      }

      average = sum/period;
      dateClose.Close = average;

      return dateClose;

      }


      Why do you get this error:



      if you have the class, member variables neednot be initialized:



      public class Test
      {
      private int temp; // this is okay.

      ..
      }


      However, if you have a local variable, then you need to initialize them:



      public void Method()
      {
      int variabl;

      sum += variable; // error.
      }


      So, local variables need to be initialized but member variables neednt be.






      share|improve this answer
























        1












        1








        1






        You can do this. If your period variable is greater than the queue count than dateClose.Close will throw an exception.



        public Models.DateClose SMAMethod (Queue<Models.DateClose> queue, int period)
        {
        decimal average, sum=0;
        Models.DateClose dateClose = null;
        for (int i = 0; i < period; i++)
        {
        dateClose = queue.Dequeue();

        if(dateClose != null)
        sum += dateClose.Close;
        }

        average = sum/period;
        dateClose.Close = average;

        return dateClose;

        }


        Why do you get this error:



        if you have the class, member variables neednot be initialized:



        public class Test
        {
        private int temp; // this is okay.

        ..
        }


        However, if you have a local variable, then you need to initialize them:



        public void Method()
        {
        int variabl;

        sum += variable; // error.
        }


        So, local variables need to be initialized but member variables neednt be.






        share|improve this answer












        You can do this. If your period variable is greater than the queue count than dateClose.Close will throw an exception.



        public Models.DateClose SMAMethod (Queue<Models.DateClose> queue, int period)
        {
        decimal average, sum=0;
        Models.DateClose dateClose = null;
        for (int i = 0; i < period; i++)
        {
        dateClose = queue.Dequeue();

        if(dateClose != null)
        sum += dateClose.Close;
        }

        average = sum/period;
        dateClose.Close = average;

        return dateClose;

        }


        Why do you get this error:



        if you have the class, member variables neednot be initialized:



        public class Test
        {
        private int temp; // this is okay.

        ..
        }


        However, if you have a local variable, then you need to initialize them:



        public void Method()
        {
        int variabl;

        sum += variable; // error.
        }


        So, local variables need to be initialized but member variables neednt be.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 23:07









        GauravsaGauravsa

        2,4151816




        2,4151816















            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values