Empty array property

Hi,

I have a class with a property that will hold an array of objects.
I want to use the constructor of my class to start of with an empty array to which I will append to later on.
But i can’t seem to declare
myArrayOfObjects = Array()

any ideas?

What’s the error you’re getting? That usually tells you something about what’s going wrong.

I think if you’re declaring an empty array you need to use new

This method requires more parameters than where passed
myArrayOfObjects = Array()

Please do not post the same question in two or more channels.

In the IDE:
Create the property and set it as follows:
Name: myArrayOfObjects
Type: Object()
Default: // leave that empty
Scope: Public // or change it to the scope it needs to have

In code:

Dim myArrayOfObjects() As Object

In both cases the array will exist and have zero elements. You do not initialize an array with New.

Doesn’t ‘Dim’ create an extra local variable?

Hey, it was an I think :stuck_out_tongue:

Yes, it does.

So it can’t be done in code?
I wanted to initialize the existing property not create an extra variable

[quote=207383:@Jan Verrept]I have a class with a property that will hold an array of objects.
I want to use the constructor of my class to start of with an empty array to which I will append to later on.[/quote]

[quote=207399:@Jan Verrept]So it can’t be done in code?
I wanted to initialize the existing property not create an extra variable[/quote]

What do you want to obtain by “initializing” ?

As a property myArrayOfObjects() As Object of your class it exists already. No need to dim it.

Just use the IDE to add a property to your class and you are done. Note the parenthesis that make the property an array.

You can then go

  myArrayOfObjects.append(new CustomObject)

Add, remove, insert any object of your liking.

For instance :

myArrayOfObjects.append(new CustomObject) myArrayOfObjects.insert(0, new Timer) myArrayOfObjects.Remove(1)

The class is part of my applications model.
I connect the objects that make up the model in the constructor of each class
I also consider it good practice to initialize every (non-object) variable including arrays, hence my question.

Every data type in Xojo has a default value:

Dim b As Boolean // implicitly set to False Dim i As Integer // implicitly set to 0 Dim d As Double // implicitly set to 0.0 Dim s As String // implicitly set to "" etc.
And for arrays:

Dim b() As Boolean // an empty array of Boolean Dim b(-1) As Boolean // equivalent to the above etc.

Your array comes pre-initialized to an empty array. There is no need to initialize it in the constructor. To reset it to an empty array, use Redim.

redim myArray (-1)