Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3688

Global Variables - Classes?

$
0
0

Hi all. I’m an absolute beginner with Xojo and come from a non-object orientated background. I’m writing a Linux app that controls a small robot. I’ve got some serial ports up and running and the UI updating. I’m using global variables for some of the info that is needed in different parts of the code (for instance, motor speeds being held in a global). I know, I know… globals are bad.

To make globals, I created a Module at the App layer, renamed it to Globals and then started adding properties in there for the various things I needed. So far so good, some integers, some strings, etc… Everything was working.

Once I got my test code going, I came back to clean things up a bit. In C, I would create a struct and then group some commons things together. Looks like I could do that with Structure in Xojo, but the recommendations was to create a Class instead. So I created a class at the App level, added a few properties to it, created a Property in my Global module pointing to the Class and then went to use that new variable in my code, and run into a nil error. So, classes are being dealt with differently then the built-in types I guess. Where am I going wrong?

Steps to reproduce:

  1. Create new desktop Project
  2. Insert → Class
  3. Rename it as ClassTest, and then add two properties (right click ClassTest on left hand bar, insert property). Rename property 1 as ‘prop1’, type integer, default value = 0. Rename property 2 as ‘prop2’, type integer, default value = 1.
  4. Left click, then Insert from the menu bar and Module. Rename as Global.
  5. Right Click Global module, add property. Rename to Test1 and type as ClassTest
  6. Right Click Global module, add property. Rename to Test2 and type as Integer
  7. Right Click ‘Window1’ and add event handler for ‘opening’
  8. In Opening, add the code :
    - Test2 = 2
    - Test1.prop1 = 2
  9. Run the code. It will fail with a NilObjectException on the test1.prop1=2 line.

So… I need to initialize my custom Class variable somewhere?

6 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 3688

Trending Articles