Suppose I have a property Session.SessionDate (As Date) and I want to assign that property the client’s local date. I do this without compilation complaints in Session.Open:
Self.SessionDate = Self.ClientTime
However, is it best to do it this way?:
Dim d As New Date(Self.ClientTime)
Self.SessionDate = d
or
Self.SessionDate = New Date(Self.ClientTime)
I sometimes get confused as to when I should be using New with dates. I have assumed that if the date already exists in the method (or exists implicitly, like Session.ClientTime), I can just assign a date variable like this:
Dim d As Date = <some existing date in the method>
But sometimes I find that even if the date exists in the method, I still must do this to make it work properly:
Dim d As New Date(<some existing date in the method>)
One particular reason. I want Session.SessionDate to be assigned the moment they started the Session. I refer to Session.SessionDate in various methods. So, say they start their Session at 11:50pm on 11/09/2018. In that case, I want Session.SessionDate to be 11/09/2018 during that entire Session, even if they remain in that Session more than 10 minutes. So they’re consistently working on the same day’s data during that Session. If I dynamically use Session.ClientTime in those methods, the day that refers to will change in 10 minutes.
[quote=413589:@Ralph Alvy]Is there a reason not to use
Self.SessionDate = Self.ClientTime[/quote]
In this case, Self.SessionDate is just a reference to Self.ClientTime. So as ClientTime changes, so does SessionDate. If you want SessionDate to remain static, use Greg’s code.