Setting the default value of a DateTime property?

I’ve created a public property of type DateTime, now I’d like to set its default value, but I’m not sure how to format the date/time in the “Default” field in the Inspector. Everything I’ve tried has caused a Syntax Error. Anyone know the actual way to do it?

I don’t think you can set it in the IDE. Set it in App.Open or Window.Open.

It is an object you would have to use New with a constructor. That isn’t supported with the Default value, which is a literal value.

I usually use a computed property and write the code to set the default value in the getter. Not sure if this is correct but works for me!
Basically if it’s null output this, else give the actual non-default value.

Thank you all for your responses. Is there any reason that the IDE should display a Default Value field for objects that don’t support one?

If you call a bug “a reason”, then yes :wink:
(my opinion).

I see two issues here.

  1. The name DateTime is potentially problematic because that’s the name of the type itself. If you try to use that in code within the object, the compiler may become confused.

  2. As was stated, objects can’t have default values that appear in the inspector because the inspector only supports the intrinsic types.

You could add a computed property that has code to convert a string into a DateTime however but I suggest that you use sqldate or sqldatetime as your format to avoid issues with people from different locales entering data differently.

5/8/23 Could be May 8th or August 5th depending on the user’s settings for instance.

i made a feature request long ago to have optional default object in properties that are just created by new …
because every objec/class property have to be initialised.
instead of the empty textbox it could be “new” to have this feature with a “bit” effort.

If only you knew how much work it would be to do that “bit”…

thats why i use “” :wink:
at least it is not impossible. for us developers it would be a great benefit.

IIRC, the amount of refactoring would make it pretty darn near impossible though, to the point that you might as well wait for the next major redesign.

@Greg_O
sometimes there is more than one solution path and maybe an easy way.
it is not my intention that xojo refactoring too much.
i faced often a impossible task and found a best way to solve it.