views:

186

answers:

4

i would like to show a TimeSpan in a MessageBox but am getting an error:

DateTime date1 = new DateTime(byear, bmonth, bday, 0, 0, 0);
DateTime datenow =  DateTime.Now;
TimeSpan age = datenow - date1;
MessageBox.Show(ToString(age));

Error 1 No overload for method 'ToString' takes '1' arguments

how do i output a messagebox with TimeSpan?

+11  A: 
MessageBox.Show(age.ToString());

Though you might not like the result. If you want a specific format you have to implement it yourself.

Joel Coehoorn
change /now/ -> /not/
ChrisF
+2  A: 

you have to do age.ToString()

PoweRoy
+1  A: 

or you can do Convert.ToString(age) to keep with the format that you have now.

Jim
+3  A: 

That's not going to look great, TimeSpan is missing a decent ToString() override on .NET 3.5 and earlier. Work around that by using the DateTime.ToString() method:

  string txt = new DateTime(Math.Abs(age.Ticks)).ToString("h:mm:ss");
  if (age.Ticks < 0) txt = "-" + txt;
  MessageBox.Show(txt);
Hans Passant