Creating an array

Morning all,
Is there a way around the following.

I want to create an array in response to a database search, but it will potentially be different each time the method is run.

I initially type the following:

Var rs As RowSet = DB.SelectSQL("select * from TABLE")
Var idArray(rs.RowCount) As Integer

Now, this did not work and told me something about needing to be a constant. I misinterpreted that and thought it needed to be something other than the rs.RowCount so I tried:

Var rs As RowSet = DB.SelectSQL("select * from TABLE")
Var num As Integer = rs.RowCount
Var idArray(num) As Integer

Same result, so I tried:

Var rs As RowSet = DB.SelectSQL("select * from TABLE")
Const num As Integer = rs.RowCount
Var idArray(num) As Integer

Still no joy. I’ve now realised that it would appear (for some unfathomable reason) that a constant HAS to be an actual value (even though rs.RowCount would be one too) AND an array MUST be a constant!
So, my question to the brainstrust is, is there a way to define my array/s such that they might have a different value each time the method is run. At the moment the only way I can do it, is to hard code a value that I would think would be higher than would ever be needed eg:

Var idArray(1000) As Integer

Bit of a waste tho.

Thanks (as always),
Barry

Maybe you are looking for:

Redim idArray(rs.Rowcount)

but if you are going to add the elements to the array, there is no need to set the size.

Hi Alberto,
I was just in the process of informing that I think I found a solution:

Var rs As RowSet = DB.SelectSQL("select * from TABLE")
Var idArray() As Integer
idArray.ReSizeTo(rs.RowCount)

This would appear to be the latest version of what you suggested. Looks like that is it. I’m just a hobbiest so I’m sure the professionals who created this language know WAY more than I do, but sometimes it seems there are just convoluted ways to do things at times. I mean, why the extra step/s?

Anyway, onward and upward… :slight_smile:

Thanks,
Barry

1 Like

What it’s telling you is that an array must be defined with a known size, probably for the compiler. So you can use a static value or a constant, but not a value computed at runtime as in your original code.

Generate the array as you go along:

Var  idArray() as Integer
Var  rs As RowSet = DB.SelectSQL ("select * from TABLE")

while  (rs.AfterLastRow=False)

  idArray.Add (rs.Column("id").IntegerValue)

  rs.MoveToNextRow ()

  wend
1 Like

Hi Tim,

Yep. Obviously adding them was the next step. I thought I’d have to dimension the array first. Some more investigating since I first posted has led me to the solution I mentioned in my reply to Alberto above as to that part. During that investigating, I also saw the solution you have posted. Thanks, that gives me the code :slight_smile:

Barry

In some cases when you know what the size the array needs to be at runtime, it is more efficient declare the array with a dimensioned then resize it to the desired size which means you don’t have to keep allocation more memory which is expensive…

That said, it only really matters for BIG arrays… IIRC Xojo allocates array memory in certain sized chunks so it does not have to allocate more memory with every add.

I would have to lookup the right method in API 2, but in API 1 you use ReDIM to reset the size of an array at run time to what you need,

-Karen

It should look like this, to empty the array there is RemoveAll:

Var myarray() As String

myarray.ResizeTo(100)
myarray.RemoveAll