GLBasic forum

Main forum => GLBasic - en => Topic started by: bigsofty on 2007-Apr-17

Title: BYREF and custom types
Post by: bigsofty on 2007-Apr-17
Why does this not work...

Code (glbasic) Select
TYPE Tmytype
foo$
ENDTYPE

GLOBAL testvar AS Tmytype

WHILE 1
myfunc(testvar)
WEND


FUNCTION myfunc: BYREF a AS Tmytype
a.foo$ = "Foo!"
ENDFUNCTION
... probably me doing something silly? I want to pass a custom type 'byref'...?
Code (glbasic) Select
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.2007.095 - 3D, NET
"test.gbas"(14) error : wrong argument type : myfunc, arg no: 1
Title: BYREF and custom types
Post by: Kitty Hello on 2007-Apr-17
Remove the BYREF. Types are passed by reference always...

That's bad, is it? It should be passed by value unless you explicitly want it, right?
Humm... Well, for now, it's just by reference.
Title: BYREF and custom types
Post by: bigsofty on 2007-Apr-17
Yep, all types should be treated the same (cept. array names and pointers).

Well at least I can go forward, Ill change it in the future if needed  :)
Title: Re: BYREF and custom types
Post by: Hemlos on 2009-Nov-22
I want to pass this array type byref, so i can store arrays with different array names.
How do you byref a type, which is declared as an array?

Code (glbasic) Select
TYPE BezArray
x
y
ENDTYPE
GLOBAL Line1[] AS BezArray
DIM Line1[NumPoints]
QuadraticBezier3dLine( #,#,#,#,#,# , Line1[] as BezArray )

Code (glbasic) Select

FUNCTION QuadraticBezierType: P0x , P0y , P1x , P1y , P2x , P2y, NumPoints , BYREF Array[] AS BezierArray
//Do stuff to the entire array type, and byref it back out to the proper array, array type.
RETURN Array[]
ENDFUNCTION
Title: Re: BYREF and custom types
Post by: MrTAToad on 2009-Nov-22
You cant return arrays as such - as they are passed BYREF anyway, you could always store back in there.

However, you can cheat if you want to pass an array back :

Code (glbasic) Select

TYPE BezArray
x
y
ENDTYPE

TYPE BezArray2
lines[] AS BezArray
ENDTYPE

NumPoints=6
GLOBAL Line1[] AS BezArray
GLOBAL Line2 AS BezArray2

DIM Line1[NumPoints]
Line2=QuadraticBezier3dLine( 1,2,3,4,5,6,NumPoints, Line1[])
DEBUG Line1[0].x+"\n"
DEBUG Line2.lines[0].x+"\n"
END



FUNCTION QuadraticBezier3dLine AS BezArray2: P0x , P0y , P1x , P1y , P2x , P2y, NumPoints , Array[] AS BezArray
LOCAL temp AS BezArray2
//Do stuff to the entire array type, and byref it back out to the proper array, array type.
//RETURN Array[]
Array[0].x=123

DIM temp.lines[6]
temp.lines[0].x=999
RETURN temp
ENDFUNCTION
Title: Re: BYREF and custom types
Post by: Hemlos on 2009-Nov-22
Thanks buddy  :coke: