Quote:
Originally Posted by ChristianNagel
Wissam,
it would help if you add the information where this code can be found. First I was not sure if is from the book, but then I found it on page 1172.
Regarding your question I didn't check what is required by the sample in the book, but the different implementations you propose produce different results.
In the first sample, if once testDate is equal to trialDate, the trialDateOK is false and thus the while loop continues with the next iteration.
This is not the case with the second exmple. Here, trialDateOK is set to true again with the next foreach iteration. trialDateOK is only set to false if the equal case is true in the last foreach iteration.
Hope this helps.
|
Thank you for the support.
In the code of the book:
Code:
private DateTime GetFreeDate(DateTime trialDate)
{
if (EventData.Count > 0)
{
DateTime testDate;
bool trialDateOK = false;
while (!trialDateOK)
{
trialDateOK = true;
foreach (DataRowView testRow in EventData)
{
testDate = (DateTime)testRow["EventDate"];
if (testDate.Date == trialDate.Date)
{
trialDateOK = false;
trialDate = trialDate.AddDays(1);
}
}
}
}
return trialDate;
}
Lets take an example,suppose we have four event dates in the the events table(
2010-10-16,2010-10-17,2010-10-19,2010-10-20)
and i pass 2010-10-16 to the GetFreeDate function
So the foreach iteration will iterate for the the first two events and set trialdate to 2010-10-18 and set trialdateok to false and the foreach iteration continues to finish the rows in the eventdata table and then the while iteration continue to iterate and then also the foreach iteration repeats the iteration in spite of that i find the available date so my comments why this additional loop.
you support is highly appreciated.
Regards.