Page 1 of 1

How to pass a structured variable to a function

Posted: Fri Nov 19, 2004 1:40 am
by CaddMan
Could someone please advise me on how to pass a structured variable to a function. It would seem that a structure can not be passed to a function directly. The documentation states that it cannot be passed to a procedure directly, unless I misunderstood. Most likely by a pointer?? I have tried several different ways which did not work.

Thanks
Ken
[/code]
*iError.w = 0

Structure Point2D
x.double
y.double
EndStructure

Dim StartLine.Point2D(1)
Dim EndLine.Point2D(1)

F64_Val(StartLine(0)\x,"0")
F64_Val(StartLine(0)\y,"0")

F64_Val(EndLine(0)\x,"100")
F64_Val(Endline(0)\y,"0")

;OpenConsole()
;PrintN(F64_Str(StartLine(0)\x, 15,#F64_Format_Decimal))
;PrintN(F64_Str(StartLine(0)\y, 15,#F64_Format_Decimal))
;PrintN(F64_Str(EndLine(0)\x, 15,#F64_Format_Decimal))
;PrintN(F64_Str(EndLine(0)\y, 15,#F64_Format_Decimal))
;Input()
;CloseConsole()

;How do I pass a structured type variable such as StartLine and Endline to
;the Function VCAddLineEntity.

OpenLibrary (0,"vcmain32.dll")
CallFunction (0,"VCAddLineEntity",@*iError,-1,StartLine,Endline)
CloseLibrary(0)

Re: How to pass a structured variable to a function

Posted: Fri Nov 19, 2004 9:19 am
by traumatic
If I'm not too tired (it's very early this morning ;)) I think your
problem lies somewhere else:

Code: Select all

Dim StartLine.Point2D(1)
Dim EndLine.Point2D(1) 

CallFunction (0,"VCAddLineEntity",@*iError,-1,StartLine,Endline)
StartLine and EndLine are 0!

Instead, you'll want to pass your arrays:

Code: Select all

CallFunction (0,"VCAddLineEntity",@*iError,-1,StartLine(),Endline())
or

Code: Select all

CallFunction (0,"VCAddLineEntity",@*iError,-1,@StartLine(),@Endline())

Re: How to pass a structured variable to a function

Posted: Fri Nov 19, 2004 9:27 am
by traumatic
CaddMan wrote:The documentation states that it cannot be passed to a procedure directly, unless I misunderstood.
I don't know what documentation you are talking about but in PureBasic you can pass structures like this:

Code: Select all

Structure MYSTRUC
  x.f
  y.f
EndStructure

Procedure YesYesBringItOn(*value.MYSTRUC)
  Debug *value\x
  Debug *value\y
EndProcedure

foo.MYSTRUC : foo\x = 0.521 : foo\y = 13.417
YesYesBringItOn(@foo)
Be careful though. Because you're using pointers here, the procedure will change the passed variable (useful for returning structured variables).

Don't know if this helps...

Posted: Sat Nov 20, 2004 1:57 am
by CaddMan
Thanks traumatic,

My problem is syntax. I am just learning how to program in PureBasic while trying to convert Visual Basic code into PureBasic code.
In VB I would have used Dim StartLine As Point2D then passed the array as StartLine. By using the function VCAddLineEntityBP (BP means By Pointer) and passing the arrays as @StartLine() for what its worth the code below works. Having to use the F64 Library to create double variables is also proving to be a challenge. There are several mouse functions that return info as a structured Point2D that I am trying to figure out how to assign to variables in PureBasic.

Code: Select all

iError.w = 0
GetState.w = 0
Toggle.w =0
lH.l=0


Structure Point2D
  x.double
  y.double
EndStructure

Dim StartLine.Point2D(1)
Dim EndLine.Point2D(1)

  F64_Val(StartLine(0)\x,"0")
  F64_Val(StartLine(0)\y,"0")

  F64_Val(EndLine(0)\x,"10")
  F64_Val(Endline(0)\y,"0")

OpenLibrary (0,"vcmain32.dll")
  CallFunction (0,"VCAddLineEntityBP",@iError,-1,StartLine(),Endline())
  CallFunction (0,"VCLastEntity",@iError,@lH)
  CallFunction (0,"VCSetCurrentEntity", @iError,@lH)
  CallFunction (0,"VCDrawCurrentEntity",@iError)
CloseLibrary(0)