setting hours on form variables

Maybe it’s something obvious. Why does this set the hours of a datetime variable to 00:00:00

	var lddate = new Date();
	lddate.setHours(0,0,0);

but this does nothing:

	this.fdstartdate = new Date();
	this.fdstartdate.setHours(0,0,0);

fdstartdate is a form variable of datetime type
It doesn’t matter if the “this” prefix is used or not. It doesn’t matter if fdstartdate is initialized as null or new Date()

Thanks for any comments.

Hi Michael,

Form variables are addressed in form methods with their name (fdstartdate) or with the ‘globals’ prefix (globals.fdstartdate).

Hope this helps.

Thanks, but it doesn’t help if I remove “this.” On the other hand either of these properly set the hours,mins, and secs to 0:

fdstartdate = utils.timestampToDate(application.getTimeStamp())

or

this.fdstartdate = utils.timestampToDate(application.getTimeStamp())

So anyway, it seems illogical to prefix a a form scoped variable with “global”. Would it not be forms.formname.fdstartdate if referred to from a method outside the form?

msedita:
So anyway, it seems illogical to prefix a a form scoped variable with “global”. Would it not be forms.formname.fdstartdate if referred to from a method outside the form?

That is correct. If you call it form another form you use forms.formName.formglobalName. But if the method is on the same form as the form variable then you can use the globals prefix or leave it out. It’s just a matter of preference I guess to use the prefix or not.

I did a little more experimenting with “globals.” and “this.”

It appears using “this.” when assigning a value creates a form scoped variable on-the-fly just like using “globals.” creates a global variable if it doesn’t already exist. I think using ‘globals.’ when referring to a form variable just creates a global variable of the same name. Using “this.” for an already existing variable within the same form is redundant.

I don’t believe this behavior is especially well documented.

msedita:
but this does nothing:

	this.fdstartdate = new Date();
this.fdstartdate.setHours(0,0,0);

Tested this out and application.output(fdstartdate instanceof Date) returns true yet the hours is not getting set. Hours do get set correctly with method and global variables. So definitely a bug with form variables. Pretty random issue you found there :)

I’m pretty sure these are the scoping rules with variables:

// globally scoped variables on the fly
x = 0
globals.y = 0

// form scope variables on the fly
this.x = 0
allvariables.y = 0

// method scope variable
var x = 0

“Pretty random issue you found there”

Yes, exactly the kind of thing that can drive one insane.

it has nolthing todo with form or global variables for both the same thing apply.
and that is that you can’t do this:

globals.mydate.setHours(xxx)

(or a form variable prefixed or not)

this is already the case (for globals no form vaiables back then) from servoy 1.0

problem with the above statement is that the call to set hours is being done on a copy of the date object in globals (or form) scope. So when changing that object you change not really the object in the global scope but some date object that is gone right away after the call.

we do this because dates are the only mutable objects (design flaw if you ask me) in java and javascripy for Value type objects (strings,numbers,booleans are the others) so we have to make a copy of the actual object because else you change under the hood suddenly the object that is also displayed in a field, or worse used in a relation query… and if you change in under thw hood we dont know it. so changing date objects in the global or formscope you have to do:

var mydate = globals.myglobaldate;
mydate.setHours(x)
globals.myglobaldate = mydate;

Oh yea, all that stupid date stuff is coming back :)

Still doesn’t explain why it works with global variables and not form variables?

global vars shouldnt also work
if that was the case then that is a bug!

priceless :)

Is this documented currently?

BTW, in ECMA script (5) it is possible to get at an obj’s mutable/immutable property and flip the bit as required. Perhaps this is something that Servoy can do in its JS wrapper?