2

I know it's obviously failing because I'm missing something here, but I can't figure out what's wrong with this line of code:

this.Duration.ToString("{0:%m} minutes {0:%s} seconds")

This is throwing a FormatException.

Hopefully somebody can point me in the right direction!

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232

3 Answers3

4

You need to use single-quotes around everything that is not a format character:

Duration.ToString("mm' minutes 'ss' seconds'");

You can either enclose the non-format characters in single-quotes or escape every single one character using the backslash. See here under the heading "Other Characters" for more info.

mirichan
  • 1,370
  • 1
  • 12
  • 25
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • I see, that's what I really want, I didn't want to use `string.Format` -- this will be the direction I go down. Mixed up the need for the specification there. Thanks a lot man! – Mike Perrenoud Dec 10 '12 at 15:01
  • Hmm. okay. I did based on assumption which is `Duration.ToString()`. But formatting should work. Anyway, it sounds like that you were waiting for this question. ;) – Kaf Dec 10 '12 at 15:03
  • Also, it appears you use single quotes `'` as the escape character? Why the deviation from `\`? More a general framework question. – Mike Perrenoud Dec 10 '12 at 15:04
  • @MichaelPerrenoud: You can either enclose the non-format characters in single-quotes or escape every single one character using the backspace. See [here](http://msdn.microsoft.com/en-us/library/ee372287.aspx) under the heading "Other Characters" for more info. – Daniel Hilgarth Dec 10 '12 at 15:07
  • @Kaf: Why do you think I waited for this question? – Daniel Hilgarth Dec 10 '12 at 15:07
  • @Kaf, I'm wondering the same thing about that comment, I'm not understanding that. – Mike Perrenoud Dec 10 '12 at 15:09
  • Well, you down voted everyone in a split of a second. Which is unusual. – Kaf Dec 10 '12 at 15:09
  • 1
    @Kaf: Well, I saw that question and the answers. IMO, all of them had some problem at that time, so I downvoted. Nothing unusual going on there. – Daniel Hilgarth Dec 10 '12 at 15:10
  • 1
    @Kaf, it's not unusual if all of the answers are relatively the same and somebody feels that's certainly the wrong approach. Don't take it personal. – Mike Perrenoud Dec 10 '12 at 15:11
  • No I am not taking it personal :) I am fine here. It was so quick. Anyway, I appreciate your comment but it is `not nice when down voting without commenting`... that is the point. Anyone can do a mistake at the first hit. Mind you this is not a competition. lol – Kaf Dec 10 '12 at 15:13
  • @Kaf: Please note: I left a comment on my downvote. I always do. – Daniel Hilgarth Dec 10 '12 at 15:16
  • I think the problem here `Anyone can do a mistake at the first hit` is that people see this site as a competition rather than a community, so they get the answer out there as fast as they can without thinking it through fully. This is becoming an alarming trend. Take your time, answer the question, and don't try and compete. People with very high rep have two things in common - length of membership is high and they answer **a lot** of questions per day. Also, people like @DanielHilgarth take more time reading the question and thinking - I've been downvoted by him too for this reason. – Mike Perrenoud Dec 10 '12 at 15:18
  • Yes you did. I meant `first comment, then down vote`. Take it easy, that is alright... – Kaf Dec 10 '12 at 15:18
  • @MichaelPerrenoud, There is a possibility an answer can be wrong even if you take time. It is not a matter of how long you've taken to answer. I did a mistake which I understand. And anyone can do it at anytime. That is reality. Don't forget that sometimes people down vote by mistake as well... – Kaf Dec 10 '12 at 15:23
  • 1
    @MichaelPerrenoud: Thanks for your nice comments, but actually I work differently: I fast-read the question, put out a first version of my answer. Re-read question, update answer. If I am not sure the code will actually work, I will now finally check it in LINQPad. – Daniel Hilgarth Dec 10 '12 at 15:26
  • 1
    @Kaf: I don't downvote if the answer looks incomplete or the error seems to be a typo or something like that. I downvote if the answer is either outright wrong or goes into a wrong direction. The latter was the case with your answer. Converting a TimeSpan to a string is simply the wrong way. That any editing of yours would fix that was very unlikely, thus the downvote. – Daniel Hilgarth Dec 10 '12 at 15:27
  • @DanielHilgarth, Yes, LINQPad. I was thinking about it. Nice to here that someone like you using it. – Kaf Dec 10 '12 at 15:28
  • Chill out guys, I think this is enough. I am going to delete all my comments and that means I am okay and happy with your answer and comments. – Kaf Dec 10 '12 at 15:31
  • @Kaf: I didn't have the feeling that anyone needed chilling here. I think we had a fruitful discussion here. I am sorry if you felt differently. – Daniel Hilgarth Dec 10 '12 at 15:34
  • 1
    @Kaf, I've been chilled, just been discussing. I think the issue is that with text you can't feel tone. – Mike Perrenoud Dec 10 '12 at 15:43
  • @MichaelPerrenoud, my feelings are alright. You started saying `Don't take it personal` which assumed that you thought I was taking it personal. I am feeling good man. You guys are up voting each others comments now. Nice !! – Kaf Dec 10 '12 at 15:57
1

If you want to use ToString, you don't have to pass the index of the value to be formatted (here %s instead of {0:%s})

I would suggest to use string.Format here. (With the index {0: [...] })

string.Format("{0:%m} minutes {0:%s} seconds",this.Duration);
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
0

Please use,

string.Format("{0:%m} minutes {0:%s} seconds", this.Duration)

instead of ToString("...")

Warr
  • 118
  • 6
  • @DanielHilgarth, oddly enough, this actually fixed it and caused it to build the string I was expecting. Hmm. – Mike Perrenoud Dec 10 '12 at 14:59
  • Why not? string.Format("{0:%m} minutes {0:%s} seconds", this.Duration) will give the correct result – Warr Dec 10 '12 at 15:00
  • @Warr: Your answer reads like a general advice. If you flesh out your answer to be actually compilable, I will remove my downvote. (Currently, my vote is locked in because it occurred over 5 minutes ago) – Daniel Hilgarth Dec 10 '12 at 15:01