A little Class confusion

I am enjoying my new found knowledge of custom Classes in Xojo, but some aspects still elude me.

I have a class (named “classSections”, not to be confused with oop classes) which has properties and methods (as I guess all classes do).

Then I have a Module which is named “sectionClasses” which contains an array named “sections” to hold each New class that I instantiate.

But this information is only required for a short time at the start of the program. So I am putting in code “sectionClasses.sections = Nil” after I am finished with it. I am assuming that this is removing that array (which is quite large) from memory. Is this the way that a Destructor works. Will Xojo eventually do a garbage collection on it?

Xojo doesn’t do garbage collection but reference counting.

If you nil something then it goes away. What does the debugger tell you if you do a peek at the variable?

The question is why you put something in a module which is sticking around when you need it only for a time? Then another place like app or a window or a separate class makes more sense. Keep everything as local as possible.

Thanks Beatrix. I guess not matter what you call it, it does go away :slight_smile:

The reason I thought I needed to store it in a Module is that it is used in two different windows whilst the program is setting itself up (based on user choices). If there is some way I don’t need a module to keep the information available, then I would like to know how.

Xojo does not do garbage collection. In Xojo class instances are reference counted. As soon as the count drops to zero, the instance is destroyed (wich means the destructor is called and after that you better not access the instance anymore…).

So if you have an array of instances of classSections and you don’t need them anymore, just empty the array:

ReDim classSections(-1)

If you do it in a method with a local array, the instances will be destroyed automatically when the function ends:

Sub SomeMethod() Dim arr() As classSections // Fill the array arr.Append(New classSections(...)) arr.Append(New classSections(...)) // Do something with these instances For i As Integer = 0 To arr.Ubound ... Next End // All instances will be destroyed here, since the array will be emptied and // there are no other references to the instances of classSection

This really depends on what you do. Without seeing your code it’s hard to say what the best solution here is. Most likely I’d use a class for instantiating everything.

It sounds like you’re on the right track, Cliff. If you don’t want to have a module just to hold this array of “sections” for the short time that you need it, you could declare sections() as a Shared Property of the “classSections” class. You’ll still need to nil the array because shared properties hang around until the reference count is zero, just like module-level properties do, but it might feel “neater” to you to do away with that separate module. :slight_smile:

Create a class do that.
Create a constructor with no parameter in both window classes and change their scope to private.
Create a constructor with one parameter of type classSection in both window classes.
Turn ImplicitInstance off for both window classes.

In the App.Open event create a local instance of classSection.
Hand it over to the window the user has chosen:

Dim win As New WindowABC(classSectionInstance) win.Show()
Read the info from the instance in the window constructor.
Set the instance to Nil (still in the windows constructor).

I’ll give it a go!

Thanks Eli