I cannot find the correct syntax to use a Public item
I have tried app.db and also the use of ps as printer
nothing I have tried works
They are both declared in the “APP” settings
App.whatever as long as they are public
To refer to properties of the App class, use the syntax App.PropertyName.
If you are already doing that but are getting a NilObectException when trying to use the property, be sure you are initializing it first with the New keyword:
App.db = New SQLiteDataBase // initialize first
App.db.SQLSelect(...) // then use
Not every time.
You can do it in the app’s Opening event, or ‘just in time’, like this
if App.db = nil then app.db = New SQLiteDataBase // initialize if nil when you get here
App.db.SQLSelect(...) // then use
Thanks very much .. I was using “as” when I should be using “=”
You can add a module and add your properties there.
Pro: No App. prefix
Con: ?
When creating App properties use Shared ones, they mare much faster to access. There can only be one either way so Shared is better. The same goes for App.methods.
I do not understand “Shared” could you explain?
If you define a class of your own you can have properties and methods. Each instance of the class has its own version of those items. So the code may look like this:
Var Elephant1, Elephant2 as New Elephant
Elephant1.legs = 4
Elephant2.legs = 3
The class can also have Shared Properties and methods. These exist only once and are accessed via the class name. No matter how many instances of the class you create there is only one of them. Code would look like this:
Elephant.NominalLegs = 4
Since there can only ever be one App object it makes no difference between shared and instance based properties. Shared are faster as Xojo does not need to obtain the instance reference to obtain the value. Yes a constant would make more sense in my example, but that doesn’t matter.
I have to say, at one time I had a whole bunch of properties at app level.
Changing them to shared properties got me a 5% speed increase at the time as I recall.
Worthwhile if they are accessed in a tight loop anywhere.