DateTime Variable doesn‘t save the value

Hi there,

I‘m pretty new to xojo and have a problem with my datetime object.

For each session in my web app I store the current time and two other datetime objects (for calculating, are also current time) which are initiated through a constructor in the session.

It should work like followed:
When I push a button it should subtract an hour from the datetime objects in the session which are for calculating, but everytime I push the button I get a Nilobjectexception because if you look at the datetime objects in the debugger there isn‘t a value instead of it - Nil.

The code looks like this:

Constructor:
Var currTime As DateTime
Var fromHour As DateTime
Var toHour As DateTime

currTime = DateTime.Now
FromHour = DateTime.Now
ToHour = DateTime.Now.AddInterval(0,0,0,1)

When I push the button a method from another class gets called to subtract one hour of fromHour and toHour.

Class Method:
Session.fromHour = session.fromHour.subtractInterval(0,0,0,1)
session.toHour = session.toHour.subtractInterval(0,0,0,1)

Webpage1.timeLabel.Text = session.fromHour.ToString + „ - „ + session.toHour.ToString

The exception is raised when it should subtract one hour in the class method.

I hope I could explain my problem good enough for you to understand it.

Probably these need to be declared as properties of the class rather than variables inside the Constructor.

Those are propertues in the session and instance through the constructor.

I think that if I would put them into the class it would display the same value for each user. I am nit sure if that‘s right what I just said but in my head it is.

I just tried it and they also don‘t save the value.

They’re not actually. For each of them, Right-click on Session and select Add to Session > Property, and declare them there. Then get rid of the Var lines from the constructor and move the rest of the lines to the Opening event. Then get rid of the Constructor.

If you write

Var currTime as DateTime

you have created a local variable. This variable is only existing/valid inside the event or method. If you leave this event or method the value of the variable is lost.
If you want to have a variable which is holding a certain value you need a static variable (see documentation for static) or you must define a global variable called property.

Or it needs to be an instance variable done by declaring it as a class property.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.