tags:

views:

1253

answers:

7

double TotalMinute=300.0 double TotalMinutesAdded=1378.0


double TotalMinute=300.0
double TotalMinutesAdded=1378.0

foreach(DataRow dr in ds.Tables[0].Rows)
       {

        //Add The above Timings to each Row's 2nd Column
        DateTime correctDate=Convert.ToDateTime(dr[2]);

        correctDate.AddMinutes(TotalMinute);
        correctDate.AddMinutes(TotalMinutesAdded);

        dr[2]=correctDate;

       }
+15  A: 

DateTiem Add* functions are not supposed to change current DateTime value. They RETURN the new Value.

If you want your value changed, type like this:

correctDate = correctDate.AddMinutes(TotalMinute);
Janis Veinbergs
Given that `correctDate` is a `DateTime`, I'm not sure what the `.Value` is doing - but your point is clear enough ;-p
Marc Gravell
+1  A: 

You have to set the correctDate variable to instance returned from the AddMinutes call:

correctDate = correctDate.AddMinutes(TotalMinute);
Todd
A: 

AddMinutes() does not change the value of the original DateTime. It returns a new DateTime with the new value that you have to assign to a variable.

Sebastian Dietz
+6  A: 

DateTime is immutable; functions like AddMinutes return a new DateTime; so you need to catch the returned value:

DateTime foo = ...
DateTime bar = foo.AddMinutes(5);
Marc Gravell
It would be really nice if the only instance member in DateTime were marked readonly since it is in fact immutable.
JaredPar
+1  A: 

DateTime is an immutable type, much like String is. You would write date = date.AddDays(1) like you would write str = str.Replace("hello", "").

efdee
A: 

Hi Sandhurst

The problem lies with

correctDate.AddMinutes(TotalMinute); 
correctDate.AddMinutes(TotalMinutesAdded);

it should be

correctDate = correctDate.AddMinutes(TotalMinute); 
correctDate = correctDate.AddMinutes(TotalMinutesAdded);

The AddMinutes method returns the result, not adding the minutes to correctDate

NR
+3  A: 

As mentioned, due to DateTime objects being immutable you have to reassign the variable.

However, a point to note is that you can chain the manipulations as so:

correctDate = correctDate.AddMinutes(TotalMinute)
                         .AddMinutes(TotalMinutesAdded);
Garry Shutler