Getting started with classes

I don’t think I truly understand classes. I’m trying to have four instances of a class that I can access globally. Here’s what I did:

I created a module, motorControl, and in it, I created:

  • a class called motors. This class has properties called limit and location
  • a method called initMotors
  • (4) properties, motor1, motor2, motor3, motor4 of type motorControl.motors

Now then, in my Window1, I’d love to do things like:
motor1. location = 123

It throws aNilObjectException when trying to access motor1.location.

Any thoughts on what I did wrong or what I should be doing instead?
Thanks!

Here’s the barebones test file: https://www.dropbox.com/s/rlyxpalgkl1tusn/Test.xojo_binary_project?dl=0

The class instance is nil unless you create a new instance of it.

So you’ve created a class called (well, I’m going to change it to the naming scheme I like) clsMotor.

When you use the motor class object you need to instantiate it for use:

dim oMotor1 as new clsMotor // the new keyword is what makes a new instance of the class to be used oMotor1.sLocation = "Inside The Thing"

So now, the oMotor1 object has the value “Inside The Thing” in it’s sLocation property. You’d be able to breakpoint and see that in the debugger.

At the end of the method, the object instance would go out of scope, so you can store it as a property of the window to maintain the reference after the method finishes. Add a property to the window, call it oMotor2 and set it’s Type: to clsMotor

When you go to use the property on the window it is also nil until you give it an instance, so you can do something similar to the above (this also works if you want to create a new instance but reuse the variable name in the same method, but that can lead to bad variable naming confusion.)

oMotor2 = new clsMotor oMotor2.sLocation = "Outside The Thing"

There’s a coffee cup metaphor that goes something like:
The class is the mold for the cup, it shows exactly how the instance should work and is the base framework for making the cup. The instance is the actual cup, you can fill it and use it; but first you have to make an instance by using the mold!

I hope this helps, and if you have any more questions feel free to ask!

without downloading your project… I would guess you made a “simple” mistake :slight_smile:

for most simple variable types (Integer, String etc) you just say

Dim myString as String

But if you do this for a class, it doesn’t “work”

Dim Motor1 as Motors

all that did was create a NIL object with the structure of “Motors”

What you need to do is

Dim Motor1 as NEW Motors

This not only creates the object/class, but calls the Constructor, which is where all the internal variables and methods are initialized

There is a WHOLE lot more to classes than just this… and once the lightbulb goes on, you will never NOT use them again :slight_smile:

Class types need to be instantiated with the NEW keyword before you can use them. Until they’re instantiated they are equal to Nil, which is why you get a NilObjectException.

motor1 = New Motors motor1. location = 123

Thanks all! Super helpful and appreciate all the great info here!!
Looks like Classes are my new crack! WOOT