Wrox Programmer Forums
|
BOOK: Professional C# 4.0 and .NET 4
This is the forum to discuss the Wrox book Professional C# 4.0 and .NET 4 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner; ISBN: 9780470502259
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 4.0 and .NET 4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2010, 09:00 AM
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Customizing the Calendar Control

Greetings,
Concering the below code:
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;
   }
i think that there is an additional loop so i suggest to place trialDateOK = true in the forreach loop so the code will become:
Code:
private DateTime GetFreeDate(DateTime trialDate)
   {
      if (EventData.Count > 0)
      {
         DateTime testDate;
         bool trialDateOK = false;
         while (!trialDateOK)
         {            
            foreach (DataRowView testRow in EventData)
            {
               trialDateOK = true;
               testDate = (DateTime)testRow["EventDate"];
               if (testDate.Date == trialDate.Date)
               {
                  trialDateOK = false;
                  trialDate = trialDate.AddDays(1);
               }
            }
         }
      }
      return trialDate;
   }
And I also suggest why we have this long code so we can replace it with short code:

Code:
private DateTime GetFreeDate(DateTime trialDate)
   {
      if (EventData.Count > 0)
      {
         DateTime testDate;
           
            foreach (DataRowView testRow in EventData)
            {
               testDate = (DateTime)testRow["EventDate"];
               if (testDate.Date == trialDate.Date)
               {                 
                  trialDate = trialDate.AddDays(1);
               }
            }         
      }
      return trialDate;
   }
I hope that what i think is logic.
your help is highly appreciated.
Thanks.

Last edited by wissam.bishouty; April 17th, 2010 at 01:17 AM.. Reason: Additonal Remarks.
 
Old October 1st, 2010, 08:03 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

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.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old October 5th, 2010, 02:31 AM
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by ChristianNagel View Post
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.

Last edited by wissam.bishouty; October 5th, 2010 at 02:34 AM.. Reason: Incorrect closing brackets for code





Similar Threads
Thread Thread Starter Forum Replies Last Post
customizing new user control dikran ASP.NET 2.0 Basics 0 September 25th, 2007 02:57 AM
Calendar control cJeffreywang ASP.NET 2.0 Basics 2 July 4th, 2007 01:05 PM
customizing calendar control vaibhav_sapre C# 0 March 11th, 2007 10:24 AM
Calendar Control with Tab Control Slicemahn Access 1 June 12th, 2006 10:35 PM
Calendar Control stu9820 ASP.NET 1.0 and 1.1 Basics 3 January 28th, 2004 11:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.