-1

My code below fails to change WALRT.

    dim WALRT as datetime = latest_weight_average_request_time
dim days_latest_weight_factor = 0.5
dim WALRT_new_hour = WALRT.hour * ( 1 - days_latest_weight_factor) +    last_request_time.hour *  days_latest_weight_factor
WALRT.AddHours( WALRT_new_hour - WALRT.hour  )

WALRT.AddHours( 4 )
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • 2
    It is a function returning a new DateTime var with the new value because [the DateTime type is immutable](https://stackoverflow.com/a/35516322/1070452) – Ňɏssa Pøngjǣrdenlarp Aug 19 '17 at 21:24
  • A part from the simple answer that AddHours doesn't change the source datatime variable but returns the new calculated value. Can you explain what are you trying to do with this code?. It isn't very clear to me. – Steve Aug 19 '17 at 21:32
  • 2
    Possible duplicate of [Why DateTime.AddHours doesn't seem to work?](https://stackoverflow.com/questions/10781420/why-datetime-addhours-doesnt-seem-to-work) – GSerg Aug 19 '17 at 21:57

2 Answers2

1

AddHours returns a new DateTime with the added hours.
It doesn't work modifying directly the variable provided

WALRT = WALRT.AddHours(WALRT_new_hour - WALRT.hour).AddHours(4)

However your code it is pretty weird because you are simply adding 4 hours to the initial value and all the lines in the middle can be removed.

For example, suppose the value of the Hour property of latest_weight_average_request_time is 10.

Your code assigns the same value to WALRT so the line that calculates the value of WALRT_new_hour is like this

dim WALRT_new_hour = 10 * ( 1 - 0.5) + 10 * 0.5 ' => equals to 10

and the following line (after fixing it) is

WALRT = WALRT.AddHours(10 - 10) ' ??

so the final line just add 4 hours to the initial value

WALRT = WALRT.AddHours(4)
Steve
  • 213,761
  • 22
  • 232
  • 286
0

DateTime.AddHoursreturns an new DateTime object. So you need to write e.g.

WALRT = WALRT.AddHours(4)
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
DogeAmazed
  • 858
  • 11
  • 28