Declare a Muli-Dimensional Dynamic Integer Array Public To The Window

Hello

This is a amateurish question with Multi-Dimensional Dynamic Arrays, I’ve only worked with single Dim Array() As Integer. I’m trying to declare a Multi-Dimensional Array with 3 integer elements Public to the window.

I’m going to dynamically populate it with the Error: Only works for one-dimensional arrays

Array.Add(Integer1, Integer2, Integer3)

I Tried:- Error.

Dim Array(, , ,) As Integer

Or In the Property Window:

Name: Array(, , ,)
Type: Integer
Default:
Scope: Public

Trying to understand the Xojo Doc Syntax below:

Var arrayName(size [, size2,…sizeN ]) As dataType

Can I get some help with the correct Syntax?

Thank You

For multidimensional arrays, you can only use ReDim.

With a multidimensional array, you have to declare at least one value per dimension.

Property
Name: Array(0,0,0)
Type: Integer

You can’t leave the commas blank

Hello Jeffery,

You can also create a mixed class as an element.
With various integer/boolean/string/image/shape entries
and assign these elements to an array.

var ele = new myele // Class array

ele.art as Element type as integer
ele.col as forecolor
ele.colb as backcolor
ele.ebene as integer
ele.licap. as boolean
ele.lineclose as boolean
ele.linend as boolean
ele.linspitz as boolean
ele.linwidth as integer
ele.name as string
ele.randein as boolean
ele.randwidth as integer
ele.rota as double
ele.scale as double
ele.sqlID as string
ele.Thoch as integer
ele.tstext as string
ele.pxs as PixmapShape
ele.pict as Picture
ele.posdx(-1) as mypoint // class = Art,x,y,color,size

var listele(-1) Array of ele

listele.add ele

Once you have declared a minimal 3-dimensional array as (0, 0, 0) of Integer – so it contains just the one element at coordinates (0, 0, 0) – you can enlarge it to any size with the ResizeTo method (formerly ReDim). Add won’t work as it is used to add an element at the end of a one-dimensional array. A multi-dimensional array has no designated end and it isn’t clear which dimension or dimensions should be enlarged. ResizeTo spells out the desired new sizes of all the dimensions and you can then assign new elements anywhere within the added space in the array.

This makes me wonder whether you don’t really want a 3-dimensional array but a 2-dimensional one with three columns? The Add statement looks like you want to add another row with three elements. Add doesn’t generalize that way though.

Hello

I had to sleep on it. I had a dyslexic mental block by staying up too late trying to solve a programming problem. I realized you got to keep them separate.

I have an old Dietel VB.Net 2008 How To Program Book. The 1st half of the book are Console examples to keep things simple.

It has an old multi-dimensional array example close to what I’m trying to achieve (rare to find). Bearing the syntax differences of outdated 2008 VB.Net to Xojo. This brings a little more light to what I’m trying to achieve and how to explain my problem.

//VB.Net Syntax book example
Dim Array1 As Integer() = (32, 27, 64, 18, 95)
Dim Array2 As Integer() = New Integer(Array1.Ubound.GetUpperUbound(0)) {}

// My Xojo Syntax attempt for a Dynamic Array Declare. Still wrong but make a better example for my question to find an answer.
Dim Array1() As Integer
Dim Array2() As Integer = New Integer(Array1())

I’m not familiar with VB.Net. Maybe you could just describe what you are trying to achieve, i.e. what kind of data you are trying to store? From your pseudo-Xojo declarations I have a hard time understanding what you are trying to do.

1 Like

Demo

myarray-test-1.xojo_binary_project.zip (6.1 KB)

Sorry I had to delete my Reply. This text editor has a hidden Reply keyboard shortcut that launched the Reply button event. I wasn’t finished and it launched.

I have 3 dynamic Integer arrays Public to the window that are dynamically populated from another loop in another method, 3 separate Integers values are loaded from a Database. Each Array1-3( i ) values are added to each integers in For Next Loop.then decreases each Integer by one until of them reaches -1 to exit the For Loop. While populating 3 new LArray1-3(). I had a brain fart thinking I could get fancy and use Multi-Dimensional Arrays.

Dim Integer1, Integer2, Integer3, LArray1(), LArray2(), LArray3() As Integer

Integer1 = rsField(Int1)IntegerValue
Integer2 = rsField(Int2)IntegerValue
Integer3 = rsField(Int3)IntegerValue

For Next i As Integer = 0 to Array1.Ubound
    Integer1 = Array1(i) + Integer1
    Integer2 = Array2(i) + Integer2
    Integer3 = Array3(i) + Integer3 
      
    Integer1 = Integer1 - 1 
    Integer2 = Integer2 - 1
    Integer3 = Integer3  -1

    LArray1(i).Add(Integer1)
    LArray2(i).Add(Integer2)
    LArray3(i).Add(Integer3)

     If Integer1 = -1 Or Integer2 = -1 Or Integer3 =-1 Then
           Exit
     End If
Next

Thank You - I’ll download it & check it out

No Blanks - Good to know Thanks Tim

As a matter of fact you could – you could use a 2-dimensional array with three columns and a variable number of rows. But then you would need to call ResizeTo for creating another row, followed by assignments to the three cells thus created. One might argue that handling three independent arrays is simpler as you only need to call Add to both increase the size of each array and to assign the new value.

Alternatively you could define a method AddRow that adds a row to a 2-dimensional array and assigns the new values. To make it sufficiently generic you would use a parameter array to pass a variable number of parameters to AddRow – something more advanced that you might try some time in your leisure hours.

You could also use a variant array that contains arrays of integers. Then you can append new integer arrays to the variant array without needing to do a ResizeTo.

The only catch is that you can’t access the integer elements directly with something like myArray(1,3)

Instead you would have to do something like:

myIntArray = myVariantArray(1)
myIntValue = myIntArray(3)

Great Suggestions - Thank You Michael & Jared

Rudolf - That Array Example you shared using a Class was great. It set off a light bulb. There’s always a better more efficient simpler way.

Thank You

The class is a cool idea.

You can combine that and the variant array to really abstract things away.
Dynamic Array Test.xojo_binary_project.zip (5.1 KB)

This can be taken to its logical conclusion with all of the array methods implemented, but I thought that this would be a fun idea to play around with.

If you want to put more than one type of class in the array, an Interface would be much safer and easier to use than a variant.

Thanks - Guys - .It all has set off a light bulb of new ideas.

The best approach would also depend on the nature of the data.

For example, the rows and columns of the array could correspond to two independent features – three columns for sex (male/female,/won’t say) and an undetermined number of rows for age, and you were counting the number of people with each combination of sex and age. A 2-dimensional array would be optimal for that kind of statistics.

But it could also be that the values in each row hang together somehow because they are referring to the same object. For example they could be the x, y, and z coordinates of points in 3D space. You may want to pass these coordinates to another function then, and that would be easiest if you stored the three values as properties of a class, and the class instances in a one-dimensional array. You could just pass the class, accessing the array just once, rather than fetching the three coordinates individually and passing them as three different parameters. Chances are you would want to store these values in such a compact fashion anyway, elsewhere in your project, so the class definition would serve multiple purposes.

If the values in a row are about the same object, more likely than not it will turn out eventually that you need to store some additional data about that object, and with a class-based storage scheme you would just add another property to the class. The array handling code wouldn’t need to be touched at all as it doesn’t care about what’s inside the classes.