c# - what's wrong with this code (.net 2.0) -


i trying latest date using code below, goes infinte loop , displays nothing in console,

    public static void sortsortymydates()     {         int = 1;         datetime[] dtlist = new datetime[20];         datetime latestdate = dtlist[1];          dtlist[1] = convert.todatetime("28/05/2013 13:00:00");         dtlist[2] = convert.todatetime("23/04/2013 13:00:00");         dtlist[3] = convert.todatetime("25/03/2013 13:00:00");         dtlist[4] = convert.todatetime("08/04/2013 13:00:00");          while(i < dtlist.length)         {             int result = datetime.compare(latestdate, dtlist[i]);              if (result < 0)                 continue;             else                 latestdate = dtlist[i];              ++i;         }          console.writeline(latestdate.tostring());     } 

you have problem loop logic:

if (result < 0)     continue; 

if result < 0 don't increment i, , loop doesn't progress.

also, comparison wrong way around. result < 0 mean currently-tested date later current maximum. reason you're getting 01/01/0001 output because you're current code finds earliest date, , of array unintialised (and therefore lot earlier test values!).

switch if instead (and remove else entirely):

if (result < 0)     latestdate = dtlist[i]; 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -