"Introduction to Programming with Xojo" page 22 Code Error

In the current version of “Introduction to Programming with Xojo” on page 22, the following code is listed:

Var today As DateTime today = New DateTime MessageDialog.Show today.Year

I get two errors:

[quote]Window1.PushButton1.Action, line 2
There is more than one method with this name but this does not match any of the available signatures.
today = New DateTime[/quote]

[quote]Window1.PushButton1.Action, line 3
Parameter “Message” expects type String, but this is type Integer.
MessageDialog.Show today.Year[/quote]

I’m sure this is a result of changes in Xojo over the last year (or more). Can someone explain what’s happening and
how I can correct.

Scott Baker

That could should instead be:

Var holiday As DateTime
holiday = New DateTime(2020, 5, 25)
MessageDialog.Show(holiday.Year)

The new DateTime class requires parameters to the constructor. If you just want the current date/time you can do use DateTime.Now.

I think your code is a hold-over (but obviously not updated) from the use of the now deprecated Date class, which was mutable.

The new API 2.0 version is the DateTime class, which is immutable, so requires different handling because it’s Date & Time value can only be set during initial assignment of the variable.

So, to fix the code example you are showing, it should look more like the following:

Var today As DateTime today = DateTime.Now MessageDialog.Show today.Year.ToString
The first line above declares the variable, the second line assigns the value and the third displays a property of the DateTime object variable.

Note: when using the old New Date syntax (with no Constructor parameters) it would return the “current” date & time. I suspect that was what was in the old version of the book. The API 2.0 equivalent is to call DateTime.Now instead.

I hope that helps to answer your question.

I know that keeping “Introduction to Programming with Xojo” up to date with changes in Xojo is a moving target, but I think that it is important for beginners to have a reliable text that they can follow step-by-step without complications.

Seriously wish it wasn’t.