A question of scope

  1. 2 weeks ago

    I'm sure this is elementary but I haven't figured it out yet.

    I have a simple program, in it a Class. In the MainWindow, I've added a property TheClass of type Class, scope Public. This does not seem accessible anywhere (run time exception NilObject). In a button action event, I cannot access it unless I declare it with Var TheClass New Class, then it works - but only in that button action after which is goes out of scope as expected. I want to use TheClass throughout the program. How do I do that and why?

    I'd have thought the adding of it as a property at the MainWindow level would have instantiated it, and as it is public it would be available.

    Thanks....

  2. Kem T

    Jan 13 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    The property is available, but the object must be instantiated. Nil is a valid value for an object property.

    Instantiate it in the Window.Open event.

  3. Bob K

    Jan 13 Pre-Release Testers, Xojo Pro, Third Party Store Kansas City

    You need to create a new instance of that class.

    Dim aClass as Class
    declares the variable but doesn't create an instance of it.

    Dim aClass as Class
    aClass = new Class

    Does.

    So somewhere in your window you have to create/load an instance.

  4. brian f

    Jan 13 Pre-Release Testers, Xojo Pro Chilly California

    Jon,

    yes if you "Var" it in a PB action event then it will only live for that event

    In the window open even just new the property as the class you created

  5. I did try, in a Window Open event:

    Var TheClass As New Class

    Same result.

    Do I need to Var the properties of the class? This Class has two properties, "name" and "motion()". I assumed they'd get instantiated along with the class instance.

    Thanks for the help by the way!

  6. Kem T

    Jan 13 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    Simply TheClass = new Class. Using var will create a local variable of the same name.

  7. Tim P

    Jan 13 Pre-Release Testers, Xojo Pro Rochester, NY
    Edited 2 weeks ago

    Also there's the Introduction to Programming Textbook that Xojo offers which goes into the specifics of instantiating classes in section 2.2 A Place For Your Stuff

  8. TheClass = New Class in the Window Open event creates compile time errors item does not exist. Also the language reference claims that:

    TheClass = New Class
    Var TheClass As New Class

    are supposed to be equivalent?

    Th Programming Textbook sheds no additional light on this....

  9. Tim P

    Jan 13 Pre-Release Testers, Xojo Pro Rochester, NY
    Edited 2 weeks ago

    @Jon F are supposed to be equivalent?

    These can do the same thing, but they're not directly equivalent. The var statement is a keyword . You can find information about what var does very early on in the textbook.

    @Jon Fitch Th Programming Textbook sheds no additional light on this....

    The answer that I just provided can be found in section 2.2 A Place For Your Stuff on page 22. I'm sure there's more in there for you to discover.

    Edit: Updated my answer to "not directly equivalent"

  10. Kem T

    Jan 13 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Answer Connecticut

    The moment you type Var, you are creating a local variable that is separate from any property you might have added of the same name.

  11. In the language reference under Var, they are suggested to be equivalent. I've read all of page 22, and doing exactly what they say, it does not work.

    Now, Kem sheds some light on it. I had to add a Property to the MainWindow, then instantiate it with TheClass = New Class. That seems to work, and begins to make some sense: the added property is equivalent to the Var statement but not limited to the Open event scope, but does not instantiate a class instance. The instantiation TheClass = NewClass is not limited in scope. Thanks Kem.

    @Kem T The moment you type Var, you are creating a local variable that is separate from any property you might have added of the same name.

  12. Tim S

    Jan 13 Pre-Release Testers Canterbury, UK

    This:

    Var TheClass As Class

    creates a variable of type class, called TheClass. Its initial value will be Nil.

    This:

    TheClass = new Class 

    initialises TheClass to point at a new object of type Class. This new object will have any properties that you added to Class, with whatever initial values you assigned to them. If you created a Constructor method for this class, then that will be executed at "new" time and may well initialise some properties to various values.

    You can do all this in one step as:

    Var  TheClass as new Class

    but personally I prefer to avoid that.

or Sign Up to reply!