XOJO Class scope

Trying to wrap my head around using XOJO classes. I’ve added a class using insert, and created a few properties and methods. I then declared a New instance in the App, in code. I have added the class as a property to the App. So far, so good. From within the app, all of my methods and properties work just fine. But if I try to call a method from a window, by casting with the App. prefix, I get a nil object exception. I know it’s a bonehead error of some kind I’m making, but I can’t seem to see where I ran off the cliff.

When you assign the new instance, how are you doing that? It should look like:

App.myClassInstance = new myClass

What do you mean by:

Propertys of kind “Object” are by default NIL / NULL
in your case you could create a new object in the app open event.

I then declared a New instance in the App, in code

example where you do this or screenshot?

I think that was it! I was declaring it differently, but the declaration was not throwing an error until I tried to actually call a method. All is well now. Thanks for your help.

2 Likes

Happy to help! Be sure to make the solution for future reference.

You don’t add a class as property to the app - you add a property of type aClass to the app. A class is just a blueprint, a set of instructions of how to build an actual instance of that class type. Consequently your app has a property (of that class type) and you refer to the property, not the class:

app has myProperty as myClass

As myProperty already is of type myClass, whether you need NEW or not depends on how you set it.

If the scope of myProperty is global / public then you can simply set myProperty’s properties with

myProperty.color = Color.red
myProperty.text = "This is the Way."

and if it is protected then you need

app.myProperty.color = Color.red
app.myProperty.text = "This is the Way."

The other way is to create a new instance of the class with the NEW keyword and set the property to this instance (which replaces the old instance):

dim prop as NEW myClass  // prop is a new INSTANCE of myClass
prop.color = Color.red
prop.text = "This is the Way."
myProperty = prop  // set myProperty to the new prop instance which replaces the old instance in myProperty

You can “combine” the two with:

myProperty = New myClass  // so you create a new class and immediately assign it to myProperty
myProperty.color = Color.red
myProperty.text = "This is the Way."
1 Like