How to add a new element to an array?

Hello all,

Its been a little while since I worked with Xojo and have forgotten how to do the simplest thing - create and use an array.

Here’s the code:

clsLassoContents - A class that has only 1 property
nodes_Lassoed() As clsLassoContents

In a method I have this code:
Dim Lassoed As new clsLassoContents
Lassoed.Nodes = i
nodes_Lassoed.Add Lassoed

In the case above, no new elements are added - the value of the .Nodes property is changed - which is not what I am after…

So, I tried it like this -
Dim NodesLassoedCount As Integer
Nodes_Lassoed(NodesLassoedCount).Nodes = i
NodesLassoedCount = NodesLassoedCount + 1

However an out of bounds exception occurs at this line
Nodes_Lassoed(NodesLassoedCount).Nodes = i

I know I am missing something, but cannot figure out what it is! I do not know in advance how many elements in the array for the property nodes_Lassoed()

nodes_Lassoed() As clsLassoContents

Any direction/help would be greatly appreciated!

Thank you all,

Are you sure? That is the correct code. How do you determine that something is wrong? Do you perhaps have other code that is affecting the array?

Walk through that section of code in the debugger. You should see that the array has grown by 1 and that the contents of the last entry has a .Nodes equal to i.

What do you mean, “the value of the Nodes property is changed”. In what way? You’re setting the Nodes property to the value of whatever “i” is just before the Add call. What does it become, and how do you determine that?

Add is a method so I assume you meant:



I muse have not have been thinking clearly -
Following receipt of your replies, I just took another look. When I was working with this code earlier I was looking at the wrong variable!

The .add function does in fact work.

My apologies for the stupidity of the post…


Hello all,

Another curious thing…

I found today that the array is growing, as it should with the .add

However, what I found is that the value of ALL of the array elements change to the last value.
So if it is set to a 0, then a 1, then a 2 in the end, ALL of their values are 2. I do not understand this.

Here’s the code. Note the remarked lines are try’s that I did to try to figure this out before posting! Note that clsLassoContents has only one property .nodes.

Appreciate any ideas all!

Dim NodesLassoedCount As Integer = 0
Dim Lassoed As new clsLassoContents

// Clean up all/any existing Nodes_Lassoed arrays
For n as integer = 0 to nodes_Lassoed.Ubound
nodes_Lassoed.Remove n
Next n

dim t as integer = 0

For i as Integer = 0 to nodes.LastIndex
If nodes(i).y > SelectionY AND nodes(i).x > SelectionX Then // Check for the top and left of each unit object to be sure inside the lasso
If nodes(i).y + nodes(i).Width < Y AND nodes(i).x + nodes(i).Height < X Then // Check the bottom and left side to be sure inside the lasso
x =x

  // Lassoed.Nodes = Nodes(i).id   
  Lassoed.Nodes = t
  // nodes_Lassoed.ResizeTo NodesLassoedCount
  nodes_Lassoed.Add Lassoed
  // NodesLassoedCount = NodesLassoedCount +1
  // nodes_Lassoed(NodesLassoedCount).Nodes = nodes(i).id
  t = t + 1
  NodesLassoedCount = NodesLassoedCount +1
End If

End If
Next i

You need to create a new Lassoed object each time you add to the array. Ie.,

Lassoed = new clsLassoContents
Lassoed.Nodes = t
nodes_Lassoed.Add Lassoed

Thanks Tim,

Maybe my new name should be Homer… Duh!