### Author Topic: Arrays in Types?  (Read 2363 times)

#### matty47

• Mr. Drawsprite
• Posts: 71
##### Arrays in Types?
« on: 2011-Nov-13 »
Are they allowed.
For instance I want to do something like this
Code: (glbasic) [Select]
`CONSTANT MaxDims=3TYPE Point     Loc[MaxDims]ENDTYPE.....................................//then later doLOCAL P1 AS POINT.......................................FOR i = 0 to MaxDims      IF P1.Loc[i] ................`Now I know this code won't work but the compiler issues an error for the Loc[MaxDims] line
Thanks
Matthew

#### Qedo

• Dr. Type
• Posts: 382
• to program what I have do how should programming?
##### Re: Arrays in Types?
« Reply #1 on: 2011-Nov-13 »
Try:

CONSTANT MaxDims=3
TYPE Point
Loc[]
ENDTYPE
LOCAL P1 AS Point
REDIM P1.Loc[MaxDims]
FOR i = 0 TO MaxDims-1
IF P1.Loc=0 THEN P1.Loc=10
NEXT

Ciao

#### matty47

• Mr. Drawsprite
• Posts: 71
##### Re: Arrays in Types?
« Reply #2 on: 2011-Nov-13 »
Tried that - didn't seem to work.
I tried changing my attack and as I only want to deal with 3D points
Code: (glbasic) [Select]
`TYPE TPoint Loc[] FUNCTION CreatePoint:x,y,z self.Loc[0] = x self.Loc[1] = y self.Loc[2] = z ENDFUNCTIONENDTYPELOCAL P1 AS TPoint`But the compiler errors the "LOCAL P1 AS TPoint" line with (25) error : command not inside function or sub

• Guest
##### Re: Arrays in Types?
« Reply #3 on: 2011-Nov-13 »
Odd - that example compiles fine here.

You do need to define the size of the array :

Code: (glbasic) [Select]
`TYPE TPoint Loc[] FUNCTION CreatePoint:x,y,z DIM self.Loc[3] self.Loc[0] = x self.Loc[1] = y self.Loc[2] = z ENDFUNCTIONENDTYPELOCAL P1 AS TPoint`

#### Ruidesco

• Mr. Polyvector
• Posts: 236
##### Re: Arrays in Types?
« Reply #4 on: 2011-Nov-13 »
Yes, you can use any kind of variable inside a type, even variables of other types as well.
A different thing is initializing  those variables, since there is no default constructor/destructor for types because they are not objects; what I do to overcome that issue is defining an "init" function for my types which I call immediately after defining them, such as:
Code: (glbasic) [Select]
`LOCAL abc as MyType; abc.init()`In "init" is where I DIM arrays, for example.

#### matty47

• Mr. Drawsprite
• Posts: 71
##### Re: Arrays in Types?
« Reply #5 on: 2011-Nov-14 »
I think I got to the bottom of my troubles. I had the following code in a file called Utils.gbas
Code: (glbasic) [Select]
`CONSTANT MaxDims=3TYPE TPoint Loc[] FUNCTION init:x,y,z DIM self.Loc[MaxDims] self.Loc[0]=x self.Loc[1]=y self.Loc[2]=z ENDFUNCTIONENDTYPELOCAL P1 AS TPoint;P1.init(1,2,3)`
with an empty main file called Test.gbas (Project Test). The compiler kept throwing an error on the creation of P1 (LOCAL P1.....line).
If I move this line to the main file I get no error. Therefore I assume that I can't declare variable of a type in an attached (referenced file) ??
Does that make sense?
Thanks for the help. I hope I can proceed now.
Matthew

• code monkey