Unable To Convert from Int Array to Int Array

Started by _PJ_, February 15, 2020, 10:50:28

Previous topic - Next topic

_PJ_

MaxIDE 1.55 [ng]

I am receiving this error.

I can only imagine that this actually related to an issue with conversion of Dimension SIZE not the basic type def of an int array itself.

can anyone help with how to correctly define the array? What is the correct syntax to set the array size within the instantiate method?

Type AType
   Field ArrayField:Int[]
   Function Create:AType(Dim1:Int,Dim2:int)I
      Local Instance:AType= New AType
      Instance.GenerateData(Dim1,Dim2)               
      Return Instance
   End Function
   
   Method GenerateData(Dim1:Int,Dim2:Int)      
      Self.ArrayField:Int[] = New Int[Dim1,Dim2]
                PopulateData(Self.ArrayField)
        End Method

_PJ_

Okay I solved it via the following - by defining explicitly dimensioned proto array in the type field:

Type AType
   Field ArrayField:Int[0,0]
   Function Create:AType(Dim1:Int,Dim2:int)I
      Local Instance:AType= New AType
      Instance.GenerateData(Dim1,Dim2)               
      Return Instance
   End Function
   
   Method GenerateData(Dim1:Int,Dim2:Int)     
      Self.ArrayField:Int[] = New Int[Dim1,Dim2]
      PopulateData(Self.ArrayField)
   End Method

Derron

You can wrap your code in {code}...{/code} blocks (replace curly with corner brackets).

In your "Field ArrayFied:Int[]" you defined it do be 1-dimensional but later wanted it to be 2-dimensional. There is also an "array of arrays" (int[][])
For more details have a look at:
https://blitzmax.org/docs/en/language/arrays/


If you have a "2 dimensional array" you might also come along wiht a "1 dimensional" by knowing the size of the "first dimension" (eg. "x").
Assume you have a "int[10,10]" then this means you have 100 entries - in a 10x10 matrix. Now you know you could have also these 10*10 in one row and identify the "rows" by dividing by 10.

So "int[100]" can store them all.
arr[0,0] becomes arr[0]
arr[5,0] becomes arr[5]
arr[9,0] becomes arr[9]
arr[0,1] becomes arr[10]
arr[5,1] becomes arr[15]
arr[9,10] becomes arr[98]
arr[10,10] becomes arr[99]

bye
Ron

_PJ_

Hi Derron,

I am still having some difficulty in passing Arrays as arguments to functions - is this possible? Is this also some confuision over the default assumption of single-dim arrays?
The documentation does not explicitly reference multidimensional arrays in this context


SuperStrict

MasterFunction(10,5)

Function MasterFunction(Dim1:Int,Dim2:Int)
Local SomeIntArray:Int[0,0]
SomeIntArray=New Int[Dim1,Dim2]

SomeFunction(SomeIntArray)
End Function

Function SomeFunction( ArrayAsArgument:Int[] )
End Function

Henri

#4
Hi,

I must admit that I have never used multidimensional arrays but this seems to work:


Code (blitzmax) Select

SuperStrict

Local ar:Int[,]

ar = New Int[5,5]

Local count:Int = 1

For Local x:Int = 0 Until 5
For Local y:Int = 0 Until 5
ar[x,y] = count
Next

count:+ 1
Next

For Local i:Int = EachIn ar
Print i
Next


Function test(ar:Int[,])
EndFunction



EDIT: Added a function.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

_PJ_


Function test(ar:Int[,])


Confirmed. This works for me!

What a strange syntax, though. It's really not too intuitive, but I do kinda get it with hindsight :)