blah blah blah is here! blah blah » Close

up1down
link

I have a day (of week) represented as a number from 0 to 6 (mon-sun). I have an hour represented as a number from 0 to 23. I have a minute represented as a number from 0 to 59. Finally, I have a duration representing the number of minutes an event lasts.

I want to add the duration to a the day, hour, minutes so that I yield an ending day, hour, and minute.

I am constructing a arbitrary date like this...

DateTime dt = new DateTime(2010, 1, 1, se.Hour, se.Minute, 0);
dt.AddMinutes(se.Duration);


...then pulling off the day (calculated by subtracting the new day from 1 as formed in the arbitrary date... this is safe because the allowable duration can not be more than one week in minutes), hours and minutes after the addition. However, adding a value of say 720 minutes does not change the DateTime dt. This seems so simple but it's not working. Thoughts?

last answered 5 months ago

1 answers

link

The problem is that, when you're adding the duration, you're not then reassigning the result to the variable dt (or some other variable). So you want:

dt  =  dt.AddMinutes(se.Duration);

Instances of the DateTime struct are immutable and so, when you add a duration to one, it produces a new instance - it doesn't change the old one.

See this recent thread for further details.

Feedback