Tech Off Thread

8 posts

Date loop backwards

Back to Forum: Tech Off
  • User profile image
    phreaks

    pointX = chart.Series["Series1"].Points.FindMaxValue("X");

    DateTime myMaxDate = maxDate;

    I want to loop backwards from a specific date... help?

    foreach
    (DataPoint dp in chart.Series["Series1"].Points)

    {

    for (DateTime myDate = myMaxDate; myDate >= maxDate.AddDays(-10); myDate.AddDays(-1))

    {

    if (dp.XValue == myDate.ToOADate())

    {

    pointX = dp;

    break;

    }

    }

    }

  • User profile image
    amotif

    DateTime is immutable. Instead of

    myDate.AddDays(-1)

    try

    myDate = myDate.AddDays(-1)





  • User profile image
    W3bbo

    phreaks wrote:
    I want to loop backwards from a specific date... help?


    Foreach uses GetEnumerator, instead you'll want to access the collection using

     for(int i=collection.Count;i>0;i--)

  • User profile image
    ScanIAm

    amotif wrote:
    DateTime is immutable. Instead of

    myDate.AddDays(-1)

    try

    myDate = myDate.AddDays(-1)







    It would be nice if immutable object operations like this would actually produce a warning when the aren't assigned to another variable.  I've had to hunt this problem down a few times in the past.

    If the compiler can figure out that I've got code that won't ever execute or notice that I'm comparing an int to a null, it should be able to find these conditions.

  • User profile image
    amotif

    ScanIAm wrote:
    If the compiler can figure out that I've got code that won't ever execute or notice that I'm comparing an int to a null, it should be able to find these conditions.


    The compiler doesn't know that in this case. All it knows is that you're ignoring the return value, which is a common idiom in code.

  • User profile image
    figuerres

    phreaks wrote:
    

    pointX = chart.Series["Series1"].Points.FindMaxValue("X");

    DateTime myMaxDate = maxDate;

    I want to loop backwards from a specific date... help?

    foreach
    (DataPoint dp in chart.Series["Series1"].Points)

    {

    for (DateTime myDate = myMaxDate; myDate >= maxDate.AddDays(-10); myDate.AddDays(-1))

    {

    if (dp.XValue == myDate.ToOADate())

    {

    pointX = dp;

    break;

    }

    }

    }



    looks to me like you can refactor that for(;Wink loop to an int...

    you are comparing n-10 to n2 which varies by -1 each step.
    inside the loop you can just do a date.AddDayes( x);
    or am i missing something??
    if not then you can simplify the code ...

  • User profile image
    phreaks

    figuerres wrote:
    
    phreaks wrote:
    

    pointX = chart.Series["Series1"].Points.FindMaxValue("X");

    DateTime myMaxDate = maxDate;

    I want to loop backwards from a specific date... help?

    foreach
    (DataPoint dp in chart.Series["Series1"].Points)

    {

    for (DateTime myDate = myMaxDate; myDate >= maxDate.AddDays(-10); myDate.AddDays(-1))

    {

    if (dp.XValue == myDate.ToOADate())

    {

    pointX = dp;

    break;

    }

    }

    }



    looks to me like you can refactor that for(;Wink loop to an int...

    you are comparing n-10 to n2 which varies by -1 each step.
    inside the loop you can just do a date.AddDayes( x);
    or am i missing something??
    if not then you can simplify the code ...


    myMaxDate is a variable date, so I would still need to deduce (n).

  • User profile image
    figuerres

    phreaks wrote:
    
    figuerres wrote:
    
    phreaks wrote:
    

    pointX = chart.Series["Series1"].Points.FindMaxValue("X");

    DateTime myMaxDate = maxDate;

    I want to loop backwards from a specific date... help?

    foreach
    (DataPoint dp in chart.Series["Series1"].Points)

    {

    for (DateTime myDate = myMaxDate; myDate >= maxDate.AddDays(-10); myDate.AddDays(-1))

    {

    if (dp.XValue == myDate.ToOADate())

    {

    pointX = dp;

    break;

    }

    }

    }



    looks to me like you can refactor that for(;Wink loop to an int...

    you are comparing n-10 to n2 which varies by -1 each step.
    inside the loop you can just do a date.AddDayes( x);
    or am i missing something??
    if not then you can simplify the code ...


    myMaxDate is a variable date, so I would still need to deduce (n).



    MyDate and MaxDate always start as the same date,
    maxDate.AddDays(-10) is a constant value for a given use of the for(;Wink so you can find that value entirely outside of that loop.

    then MyDate just goes -= 1 on each pass till it becomes == to or less than the const maxDate.AddDays(-10)
    so do a couple of operators and find out how many days that is and makem the loop an int loop
    TimeSpan Gap = date1 - date2;
    Gap.Days gives # of days to use in the for loop.

    if I was doing this I could write it up very nice.... clean and very easy to follow...
     I'd have to also look at your if() and see how that all works out.

    hey just my thoughts, I am not saying it's "horrible" by any means...

    just that as I started with 6502 cpu's and old PC XT's I still keep an eye out for stuff like using an int when i can.

    and I write .net code for the compact framework so when I code on them I generaly have a lot less ram and a slower cpu so I get very carefull, they can slow down sometimes for almost no reason.
    so I tend to apply my own rules of "KISS" on code. Smiley

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.