7

I'm trying to finish a quick "demonstration of learning" program by morning for Mothers Day. I've created a textbox for my mom to enter my birthday and a label to display the number of years, months, days, and seconds I've been alive when she clicks a button.

The following is the part of my code where I am stuck:

private void button1_Click(object sender, EventArgs e)
{
    DateTime  sonsBirthday = DateTime.Parse(txtSonsBirthday.Text).Date;

    DateTime now =  DateTime.Now;

    TimeSpan timeSpan = now - sonsBirthday;
    timeSpan = Convert.TimeSpan(lblTimeAlive); // blue squiggly under TimeSpan here

As I commented in the code, I get a blue squiggly under TimeSpan in the last line; but I don't understand why. What am I doing wrong?

I'm just a student: so I've got the concept but am not used to datetime formats and need a little help.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
J.S. Orris
  • 4,653
  • 12
  • 49
  • 89
  • Now that's an inventive Mother's Day gift! – J0e3gan May 12 '13 at 04:37
  • Does the code not build? Does a runtime exception occur? Is any information regarding the issue displayed when you hover your mouse pointer over the blue squiggly? – J0e3gan May 12 '13 at 04:39
  • 2
    I think you may have the last line logically backwards. Don't you want to assign a text representation of `timeSpan = now - sonsBirthday` to `lblTimeAlive.Text`? – J0e3gan May 12 '13 at 04:43
  • 1
    This is correct, I want to parse it the label and I get the following exception: Error 1 'System.Convert' does not contain a definition for 'TimeSpan' – J.S. Orris May 12 '13 at 04:47
  • 2
    Did you mean to do this: `lblTimeAlive.Text = timeSpan.ToString();`? This displays the total time alive in the label. – pcnThird May 12 '13 at 04:47
  • Second of all I learned Console. Not Win Forms. I just had previous experience with WinForms – J.S. Orris May 12 '13 at 04:49
  • I want to show the exact time i've been alive thats all I need. Then a picture of me is going to pop up with other things for her to enter that will end in a heart warming message. – J.S. Orris May 12 '13 at 04:51
  • Yeah, it is bad enough that Java in CS curricula to the exclusion of more foundational languages has made understanding pointers and explicit memory management "black arts"; God help us if WinForms is in the mix too. :} – J0e3gan May 12 '13 at 04:51
  • 1
    @KenWhite: The message you suggest is a feature for Sprint 2; this is also a demonstration of progressive elaboration and agile development he has learned. ;) – J0e3gan May 12 '13 at 05:02

1 Answers1

7

Try something like this:

private void button1_Click(object sender, EventArgs e)
{
    DateTime  sonsBirthday = DateTime.Parse(txtSonsBirthday.Text).Date;

    DateTime now =  DateTime.Now;

    TimeSpan timeSpan = now - sonsBirthday;
    //timeSpan = Convert.TimeSpan(lblTimeAlive); // old
    lblTimeAlive.Text = timeSpan.ToString(); // new

Then fine tune the string formatting for timeSpan.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • Thank you! That other guy was pestering me about me doing homework (SWEAR IT IS NOT!) Im just trying to create a program for my mom for Mothers Day! Thanks for your help. I can do everything else I wanted to do now. – J.S. Orris May 12 '13 at 04:55
  • 2
    @JeffOrris: No problemo. Like I always say, it's your loss if it is for your homework. The idea is not to learn details that make sense in the particular language/API/framework you are working with as a student as much as to learn how to think critically and how to learn - to thrive through certain change that will replace the details with new ones. I will take your word, though. ;) – J0e3gan May 12 '13 at 04:58
  • Unfortunately the "Last Step" is impossible. A year is not a fixed period of time, it also depends on which calendar you use. – Aron Nov 10 '15 at 17:23