Date References

From REALbasicWiki

Jump to: navigation, search

When working with Date objects be aware that comparison and assignment is handled differently by the compiler.

A comparison works on objects, an assignment on references to objects.


Example 1:

Dim d1 As New Date
Dim d2 As New Date
 
If d1 = d2 Then

In example 1 the actual Date objects' contents are compared as expected. The comparison is true if both objects were created within the same second.


Example 2:

Dim d1 As New Date
Dim d2 As New Date

d1.Year = 2011
d2.Year = 2012

d1 = d2

d1.Year = 2013
 
MsgBox Str(d1.Year) // delivers 2013
MsgBox Str(d2.Year) // delivers 2013 too

Here the assumed action is not copying the content of the Date object referenced to by d2 over the content of the Date object referenced to by d1, but the reference d1 is set to the reference d2, so both references point to the same Date object.

To achieve the desired copy use

d1.TotalSeconds = d2.TotalSeconds

Or, better yet,

dim d2 as new Date(d1).
Personal tools
related