Get DateTime in one line, in REALbasic

In REALbasic syntax, it is not legal to call a method on an instance created with "New". So you have to capture the new object on one line of code, and then on successive lines use that object. This is particularly inconvenient when briefly needing the current datetime instance or string. Grabbing the datetime is common when logging, debugging, or recording the moment in database records.

For example, using getting a datetime as a string requires two lines of code:

dim now as new Date
MsgBox( "At the tone the time will be: " + now.SQLDateTime )

For convenience, to collapse those multiple lines of code, add these two little methods to your App class, or some other utility class.

Function now() As Date
  dim thisMoment as new Date
  return thisMoment
End Function


Function nowSQLDateTime() As String
  dim thisMoment as new Date
  return thisMoment.SQLDateTime
End Function

Now you can make get the datetime mid-line:

MsgBox( "At the tone the time will be: " + app.nowSQLDateTime )

No comments:

Post a Comment