Initialize Rect in an array

enonod

Well-Known Member
Licensed User
Longtime User
How do I declare part of an array to be a Rect please?

I cannot see how to initialize the Rects in a loop since they are not declared as Rects before the loop.

Dim kRows=3, kCols=3, row, col as int
Dim aThings(kRows, kCols, 2)

For row=0 to 3
For col=0 to 3
aThings(i,j,0).initialize(L,T,R,B)
aThings(i,j,1)=colors.Blue
Next
Next
 

klaus

Expert
Licensed User
Longtime User
Could you be more precise on what exactly you want to do.

What type is the array aThings in Dim aThings(kRows, kCols, 2) ?
aThings(i,j,0) must be a Rect
aThings(i,j,1) must be an Int.
You could define a custom type for these and then define an array of this type.

Where do the values L, T , R and B come from ?

Best regards.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks for looking klaus. Sorry it is not clear, I am not finding it easy to explain. I did say 'part' of array to be Rect. The l,t,r,b are the Rect rectangle left etc.
The array has integers row col which are used to access each of the Things, colour and a Rect for each combination of row, col. There might be additional Things that I will add to the array for each row/col position.

I know how to dimension and use arrays but not how to initialize one part for an object.

I require, for each combination of row/col to store the colour and the Rect object. They are dissimilar things in the same array. Had they been Int's and strings there would be no problem, I believe I can store a mixture of anything in an array.

Here one is an object that has to be initialized and that is the difficulty.

I did have separate arrays for colour and Rects associated with the same row/col in each but it seemed an overkill when I 'should' be able to mix 'things' in a common array.

I want to use Canvas.DrawRect(aThings(row, col, 0), aThings(row, col, 1) ...)
not two arrays... Canvas.DrawRect(aRects(row, col), aColours(row, col)...)
and
Dim dRectA as Rect
dRectA.Initialize(...)
aThings(row, col, 0)= dRectA

does not work.

I found that as I traversed row/col the result in the debugger said not initialized.
I want to initialize aThings(row, col, 0) for each row/col to be a 'Rect' and each aThings(row, col, 1) is to be an Int for colour not also a Rect.

Probably still not clear.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
[EDIT] No NOT solved! I cannot delete this message. The array aRect has been preset to Rect. If it was not it would not work. I want aRect(col,row,0) to be RECT and aRect(col, row,1) to be colour.
Please help.


I seem to have solved it and realise why you could not understand. When trying to initialize the part of the array I did not have the 3rd dimension.
The following now seems to work. The missing was the '0' in aRect(col,row,0)...
But if you see something wrong please say.:)
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cvsGrid As Canvas
   Dim aGrid(6,6),col,row,kWH=20dip As Int
   Dim aRect(6,6,2), dRectA As Rect
   cvsGrid.Initialize(Activity)
   For row=0 To 5
      For col=0 To 5
         dRectA.Initialize((col+1)+col*kWH,(row+1)+row*kWH,(col+1)+col*kWH+kWH,(row+1)+row*kWH+kWH)
         cvsGrid.DrawRect(dRectA,Colors.Blue,True,1dip)
         aRect(col,row,0).Initialize((col+1)+col*kWH,(row+1)+row*kWH,(col+1)+col*kWH+kWH,(row+1)+row*kWH+kWH)
         aRect(col,row,1)=Colors.Blue
      Next
   Next
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are:
B4X:
Sub Process_Globals
    Type Things(Rect As Rect, Color As Int)
    Dim NbRows = 6, NbCols = 6 As Int
    Dim aThing(NbRows, NbRows) As Things
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim row, col As Int
    Dim kWH = 20dip As Int
    Dim cvsGrid As Canvas

    cvsGrid.Initialize(Activity)
    For row=0 To NbRows  - 1
        For col=0 To NbCols - 1
            aThing(row, col).Initialize
            aThing(row, col).Rect.Initialize((col+1)+col*kWH,(row+1)+row*kWH,(col+1)+col*kWH+kWH,(row+1)+row*kWH+kWH)
            aThing(row, col).Color = Colors.Blue
            cvsGrid.DrawRect(aThing(row, col).Rect,aThing(row, col).Color,True,1dip)
        Next
    Next
End Sub
Best regards.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Well I thought I was doomed. Thank you so much for this solution. I would never have followed this path and I will study it very carefully and learn.
I will also, now, look at what maps are.
I don't know where I got the idea of mixed arrays, I must have been reading Types.
Thanks both.
 
Last edited:
Upvote 0
Top