April 02, 2020, 10:40:48 AM

Author Topic: Unable To Convert from Int Array to Int Array  (Read 319 times)

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Unable To Convert from Int Array to Int Array
« on: February 15, 2020, 10:50:28 AM »
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

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Re: Unable To Convert from Int Array to Int Array
« Reply #1 on: February 15, 2020, 11:31:07 AM »
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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2818
Re: Unable To Convert from Int Array to Int Array
« Reply #2 on: February 15, 2020, 01:33:55 PM »
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

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Re: Unable To Convert from Int Array to Int Array
« Reply #3 on: February 16, 2020, 10:35:54 AM »
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

Code: [Select]
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

Offline Henri

  • Full Member
  • ***
  • Posts: 225
Re: Unable To Convert from Int Array to Int Array
« Reply #4 on: February 16, 2020, 11:43:08 AM »
Hi,

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


Code: BlitzMax
  1. SuperStrict
  2.  
  3. Local ar:Int[,]
  4.  
  5. ar = New Int[5,5]
  6.  
  7. Local count:Int = 1
  8.  
  9. For Local x:Int = 0 Until 5
  10.         For Local y:Int = 0 Until 5
  11.                 ar[x,y] = count
  12.         Next
  13.        
  14.         count:+ 1
  15. Next
  16.  
  17. For Local i:Int = EachIn ar
  18.         Print i
  19. Next
  20.  
  21.  
  22. Function test(ar:Int[,])
  23. EndFunction
  24.  


EDIT: Added a function.

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

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Re: Unable To Convert from Int Array to Int Array
« Reply #5 on: February 16, 2020, 01:18:25 PM »
Code: [Select]
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 :)

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal