Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 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 December 12th, 2009, 12:49 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default Age

I am having trouble with exercise 5 in chapter 5. You ask to calculate the users age from them entering their birth date.

I have the following code which I am confident should work but when I input my values I keep triggering the validation I have set up

Code:
int age;
        DateTime birthday;
        bool flag;

        //Get the current date
        DateTime currentTime = new DateTime();
        currentTime = DateTime.Now;

        //Check the input
        flag = DateTime.TryParse(txtBirthday.Text, out birthday);
        if (flag == false)
        {
            MessageBox.Show("Please use the format, MM/DD/YY", "Input Error");
            txtBirthday.Focus();
            return;
        }
        age = currentTime.Year - birthday.Year;
        lblAge.Text = age.ToString();
Am I inputting in the values wrong? Or have I done something wrong in the code?

Values I have tried inputting are as follows:

MM/DD/YY
MM/DD/YYYY
M/DD/YYYY

And then the same again without seperators and then using commas as seperators.

Obviously I am replacing the letters with corresponding numeric values but I was pretty confident what I had written would work and I am confused as to why it isn't.

Most likely I am being a numpty and just need another pair of eyes to point it out to me. Any help would be appreciated.
 
Old December 12th, 2009, 01:27 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What's the culture / regional settings of your machine? TryParse takes into account culture specific settings, so maybe you need DD-MM-YYYY or DD/MM/YYYY instead?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 12th, 2009, 01:49 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Got it in one, as I am in the UK my regional settings were different so entering the details correctly as DD/MM/YY worked.

Cheers,
 
Old December 12th, 2009, 01:57 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In my (European) opinion, DD/MM/YY or DD/MM/YYYY makes a lot more sense anyway.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 12th, 2009, 02:18 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Definitley





Similar Threads
Thread Thread Starter Forum Replies Last Post
Age Calculation rgerald SQL Server 2000 32 October 10th, 2007 01:12 AM
Getting Respondents Age from Dates Juan Patrick Visual Basic 2005 Basics 1 July 3rd, 2007 06:55 AM
age function keyvanjan ASP.NET 1.0 and 1.1 Basics 2 August 30th, 2006 08:46 AM
Vacancy Age alannoble26 Excel VBA 2 November 23rd, 2005 03:05 AM
calculating a persons age Hudson40 Access VBA 4 January 26th, 2005 11:45 AM





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