BYREF and custom types

Previous topic - Next topic

bigsofty

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
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

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.

bigsofty

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  :)
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Hemlos

#3
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
Bing ChatGpt is pretty smart :O

MrTAToad

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

Hemlos

Bing ChatGpt is pretty smart :O