2

New to DateTime and google is sincerely failing me. I'm currently using a DateTime variable to track time in my game.

The problem is, is that any attempt of changing said DateTime is resulting in a "Property [time] is ReadOnly" error. Any help, working alternatives or roundabouts would be appreciated.

Sub IncreaseTime(unit As Integer)
    Dim NewDate As DateTime

    With World.WorldTime
        NewDate = New DateTime(.Date.Year, .Date.Month, .Date.Day, .Date.Hour, .Date.Minute, .Date.Second + unit)
        .Date = NewDate
    End With
End Sub

Edit #1;

   Sub IncreaseTime(unit As Integer)
    Dim NewDate As DateTime

    With World.WorldTime
        NewDate = NewDate.AddSeconds(unit)
        .Date = NewDate
    End With
End Sub

Edit #2;

Image Image2

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Shaun Greatrix
  • 85
  • 1
  • 11
  • 2
    ALL the properties are read only: Date, Year etc. `NewDate.AddMinutes(10)` or `NewDate.AddHours(-1)` to subtract – Ňɏssa Pøngjǣrdenlarp Feb 19 '16 at 22:00
  • 1
    Adding to what @Plutonix has said, `.AddMinutes()` and friends will not *change* the value of your `DateTime` variable. Rather, you'll want to assign the new value to your variable, as in `NewDate = NewDate.AddMinutes( 10 )` – Bob Kaufman Feb 19 '16 at 22:01
  • Yes, sorry; I forgot to mention a DateTime is immutable - assign the result to the same or new var. good catch @BobKaufman – Ňɏssa Pøngjǣrdenlarp Feb 19 '16 at 22:04
  • Please see my edit - so what would I replace ".date = newdate" to? How do you "Reassign" it as such? – Shaun Greatrix Feb 19 '16 at 22:05
  • Re: EDIT - that `NewDate` is rather pointless - declared locally, it will go out of scope when the method ends. Your code is correct - the var has *local scope* only. If you want to use it elsewhere, decalre it outside the sub. [This answer](http://stackoverflow.com/a/33249045/1070452) explains scope – Ňɏssa Pøngjǣrdenlarp Feb 19 '16 at 22:06
  • Edited again; strangely, I can update NewDate just fine as it's local, but I can't change a public variable in a public class? – Shaun Greatrix Feb 19 '16 at 22:10
  • you cant assign a value to hours, minutes, years or date. The first line (edit 2) creates a NewDate +unit seconds from World. The second line is FUBAR. If your plan is to change the datetime of `World` : `WorldDate = WorldDate.AddSeconds(unit)` – Ňɏssa Pøngjǣrdenlarp Feb 19 '16 at 22:11
  • So back to the original question, what am I missing here to update my world.worldtime.date DateTime variable? – Shaun Greatrix Feb 19 '16 at 22:14
  • Apologies; still, isn't that exactly what I've done in my FUBAR line? .date = .date.addseconds(unit) ? – Shaun Greatrix Feb 19 '16 at 22:18
  • 1
    No. `Date` is an immutable property of a DateTime variable (the leading dot means it is a property). You cannot assign values to any of them. – Ňɏssa Pøngjǣrdenlarp Feb 19 '16 at 22:20
  • You're kidding me. So it's literally; Sub IncreaseTime(unit As Integer) : World.WorldTime = World.WorldTime.AddSeconds(unit) : End Sub – Shaun Greatrix Feb 19 '16 at 22:22
  • If you ask me, you dont need a sub to encapsulate that, but yes, you create a whole new DateTime var when you Addxxx any unit. – Ňɏssa Pøngjǣrdenlarp Feb 19 '16 at 22:24
  • 1
    No wonder I couldn't find google results, it's simple. Thanks guys. Do one of you want to post the Answer and we can close it, for future searchers? – Shaun Greatrix Feb 19 '16 at 22:26

1 Answers1

5

The DateTimetype is immutable - all the properties are read-only, so you cannot set the Date, Time or even a part of them (years, mins etc).

Since none of the properties can be set directly, to change any part of one use one of the methods to AddSeconds(), AddYears() etc. These all create and return a new DateTime value with the desired adjustment. To subtract, just use a negative value:

WorldDate = WorldDate.AddMinutes(unit)
' to subtract:
WorldDate = WorldDate.AddMinutes(-unit)

The result must be assigned, either to a new variable or the existing one.

The reason is pretty simple: adding n minutes might roll over the Day or Month as well as other properties like DayOfYear or DayOfWeek. Allowing direct access would require lots of checking that the value is legal (like .Minutes = 67 or .Day = 33).

To set the date or time to specific values, create a new one using some of the old values and specifying the other values you want:

WorldDate  = New DateTime(WorldDate.Year, WorldDate.Month, WorldDate.Day, 
         myHrs, mySecs, myMins)
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178