Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Professional C# 4.0 and .NET 4
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
  #2 (permalink)  
Old October 1st, 2010, 08:03 AM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
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
  #3 (permalink)  
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.