How to get unknow multidimension array from Method?

I have a method (GetTypeCbte()) that return a multidimension array but the size of the array is not fixed.
How can I get the value from it?

I’m doing this:

dim newArray(100,3) as String  //I only know it return less than 100 items
newArray=wsaa.GetTypeCbte()

and it works, but surely it’s a right way to Dim or ReDim the newArray() to the size of the returning array, isn’t it?

Maybe:
dim newArray(0,3) As String
Dim ArrayCnt As Integer

newArray.Append wsaa.GetTypeCbte()
ArrayCnt = ArrayCnt + 1

Doing that will gives you the number of Cells in the Array and you do not try to write above 100… to lead in an error :wink:

This throws away your original array and replaces it with a totally new array. How does GetTypeCbte construct the array? That will help us advise you better.

I don’t understand this. Append is for one-dimensional array. And what is ArrayCnt for?

newArray() is only for parsing the return from method. GetTypeCbte() generates a multidimensional string array from a XML document, ie, response(i,j) and returns it. Method return type is String(,)

Awesome. So the line

dim newArray(100,3) as String  //I only know it return less than 100 items

is irrelevant because GetTypeCbte will return its own array. The question is, how does GetTypeCbte define the array? Is it a static definition like newArray(100,3) or do you start with newArray(-1,-1) and build the array dynamically by appending to it?

I see. The method defines its returning array this way:

Dim response(-1,-1) as String

Dim nodesCbteTipo as XmlNodeList
nodesCbteTipo=nodeSoapBody.XQL("//CbteTipo")  //nodeSoapBody is a predefined XmlNode
if nodesCbteTipo.Length>0 then
  Dim totNodes as Integer=nodesCbteTipo.Length-1
  Dim totTags as Integer=nodesCbteTipo.Item(0).ChildCount-1
  ReDim response(totNodes, totTags)

  for i as Integer=0 to totNodes
    for j as integer=0 to totTags
      response(i,j)=nodesCbteTipo.Item(i).Child(j).FirstChild.Value
    next
  next

Return response
1 Like

your return type should be
string(-1,-1)

theres no way in xojo to create an arbitrarily dimensioned array from nothing at runtime

ie there is no magical

    dim a as new array( .. now what ????? )

the number of dimensions is for anything defined IN xojo code, known at compile time

You can use UBound (now LastRowIndex) to get the size of each dimension.

newArray = GetTypeCbte()
for i as Integer = 0 to newArray.LastRowIndex(1)
   for j as Integer = 0 to newArray.LastRowIndex(2)
      // some code
   next
next

I did this:

dim newArray(-1,-1) as String=GetTypeCbte()  //This is what I was doing wrong

for i as integer=0 to Ubound(newArray(),1)
  for j as integer=0 to Ubound(newArray(),2)
    TextArea1.Text=TextArea1.Text+arr(i,j)+"-"
  next
  TextArea1.Text=TextArea1.Text+EndOfLine
next()

Thank you all!