Date Property on Form

Hi I have 2 Date properties on a form.
mdtValidTo & mdtOrigValidTo

Question is why are they both set to 9/30/19 after the code runs?

This must be caused by them being a property of the form and not being Dimed as New Date.

How do I get around this.


mdtOrigValidTo = mdtValidTo          <<<<<<<<<<<<<<<<<<< They are both set to 9/30/18


if mnMultiYear = 2 then
  mdtValidTo.year = mdtValidTo.Year + 1   <<<<<<<<<<<<<<<<<<< mdtValidTo is set to 9/30/19
Elseif mnMultiYear = 3  then
  mdtValidTo.year = mdtValidTo.Year + 2
End

                           <<<<<<<<<<<<<<<<<<< They are both set to 9/30/19

Well if it was being set to today’s date I’d have said they were initialized when created by the form. But the fact that it is a date in the future has me stumped.

I can’t find the information but I think that when you do

mdtOrigValidTo = mdtValidTo 

on a Date objet, you are telling mdtOrigValidTo that will be the same as mdtValidTo and not what you think as “only copy the date from mdtValidTo to mdtOrigValidTo”.

So every time mdtValidTo changes, then mdtOrigValidTo will change.

You can change your code to:

mdtOrigValidTo.SQLDate = mdtValidTo.SQLDate

or

mdtOrigValidTo.SQLDateTime = mdtValidTo.SQLDateTime

and it will work as you want.

Edit: am I wrong? or maybe I didn’t understand the problem?

[quote=406462:@Richard Albrecht]Hi I have 2 Date properties on a form.
mdtValidTo & mdtOrigValidTo

Question is why are they both set to 9/30/19 after the code runs?

This must be caused by them being a property of the form and not being Dimed as New Date.

How do I get around this.

[code]

mdtOrigValidTo = mdtValidTo <<<<<<<<<<<<<<<<<<< They are both set to 9/30/18

if mnMultiYear = 2 then
mdtValidTo.year = mdtValidTo.Year + 1 <<<<<<<<<<<<<<<<<<< mdtValidTo is set to 9/30/19
Elseif mnMultiYear = 3 then
mdtValidTo.year = mdtValidTo.Year + 2
End

                       <<<<<<<<<<<<<<<<<<< They are both set to 9/30/19

[/code][/quote]
Because mdtOrigValidTo and mdtValidTo are both objects of type Date. When you write:

mdtOrigValidTo = mdtValidTo 

both of those variables point to the same place in memory and changing one therefore changes both.

Dates are classes. Assigning them doesn’t create two different things; it creates two references that point to the same thing.

This is explained more here:
http://developer.xojo.com/userguide/classes

That’s what I figured happened as I experienced this before, though not with form properties but with local variables.

To solve that I had to use Dim xxx as New Date for all of them.

So how can you have a 2 date properties on a form?

Well, like Paul said dates are a class and you have to treat them as such. Use new to create the instance of the class. If you put it on the window as a property it’s a NIL object until you initialize it somehow.

If mdtOrigValidTo is not nil (say, read from a database) then you could do something like this:

mdtValidTo = new Date(mdtOrigValidTo) //Automatically initializes the date object with the one passed in but yet are separate instances

or alternatively you could do this:

mdtValidTo = new date //Create the instance mdtValidTo.TotalSeconds = mdtOrigValidTo.TotalSeconds //Set value to that of original

@Alberto De Poo

Your solution worked, but not sure why after reading the other posts.

@Bob Keeney

I going to use your method as it makes sense.

Thanks all!

Richard, it works because you are “changing” the values inside the class/object.

Without .TotalSeconds or .SQLDateTime your are telling one object to be the same as other, so they share the same space.

Well is hard for me to explain this in Spanish, so imagine how hard is for me to explain in English.

There is a good explanation in the link Paul provided, including helpful diagrams. See the section “Understanding Classes, Instances and References”.

[quote=406466:@Paul Lefebvre]This is explained more here:
http://developer.xojo.com/userguide/classes [/quote]