Multidiminsional Arrays and Dim

Hi Folks,

I’m trying to assemble a multidimensional array At the top of the method, It is a global property Dimmed to (-1, -1). After I have the results of a command, I then try to ReDim it in the method to (theDeviceCount, 4) with theDeviceCount being the number of devices in the list returned. The code is:

// scanResults is a string array containing an even multiple of 5 lines for each device
Dim theDeviceCount As Integer = scanResults.Ubound \\ 5
Redim DeviceList(theDeviceCount, 4)

But, that always ends up in a compile error telling me that the size of an array must be a constant or a number.

Since I don’t know how many instances I will have until I’m running, how can I set a constant or number?

See this thread:

https://forum.xojo.com/15513-assigning-array-size/0

I would explain in more detail but I’m viewing this on my phone.

[quote=365349:@Nick Alonge]See this thread:

https://forum.xojo.com/15513-assigning-array-size/0

I would explain in more detail but I’m viewing this on my phone.[/quote]
Thanks, Nick, but that assumes we know what the value for the Dim/ReDim would be. In this case, I don’t until the app is running.

Yes, I’m sure - or at least that is where the debugger tells me the error is.

[quote]strange, i have made simple test, work well

dim a(10,10) [/quote]

In your example, what is a?

As has been pointed out, when initially dimming a multi-dimensional array, you must use a literal.

dim myArray(-1, -1) will suffice.
But when redimming said array, one can use a variable.

redim myArray(a, b)

something else must be going on in your code.

I have tried every combo I can think of and cannot duplicate that error
except when a variable is used during the initial DIM statement
DIM must have constants as Roger mentioned

DIM a(10,10) as integer // this is fine
dim x as integer=10
dim a(x,x) as integer // this will produce said error
dim x as integer=10
dim a(-1,-1) as integer
redim a(x,x)  /// this works

No matter how I sort it, I continue to get this error. Here is the exact code again:

Dim theDriverCount As Integer
theDriverCount = scanResults.Ubound \\ 5
Dim DeviceDrivers(-1, -1) As String
Redim DeviceDrivers(theDriverCount, 4) As String

The difference between my code and all of the examples that you are providing is that I’m doing an integer division to get the value of theDriverCount. Other than that, I see no difference in the logic in my code and all of your examples.

I get Syntax Error on this line:

Redim DeviceDrivers(theDriverCount, 4) As String

my guess is that that line really doesn’t have the last ‘As String’, right?

Edit:
I did the same test as Wayne on Mac; no error

This code works for me on Windows

Dim scanResults(25) As String Dim theDriverCount As Integer theDriverCount = scanResults.Ubound \\ 5 Dim DeviceDrivers(-1, -1) As String Redim DeviceDrivers(theDriverCount, 4)

All I’ve done is add an array to get a ubound for the calculation.

Wayne has the answer . . . callout the type with dim, but not with redim

I don’t think so. The original post doesn’t set the type on the redim, and the error message is different.

DIM must specifiy the datatype, and must have literal values (not variables) for the bounds
REDIM should not (and can not), and can either variables or literals for the bounds
REDIM cannot be used unless DIM was used FIRST

D’oh - so many “iterations” and that simple oops was the actual error. However, the error message that Xojo displays is very misleading :frowning:

Thanks all!

Okay - I take it back - the Xojo error is exact - my frustration with battling the original issue just had me seeing the error and not what it actually said when it changed from the type error to the syntax error.