How to pass a structured variable to a function

Just starting out? Need help? Post your questions and find answers here.
CaddMan
User
User
Posts: 21
Joined: Sat Jul 10, 2004 8:05 pm
Location: USA (East Coast)

How to pass a structured variable to a function

Post 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)
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: How to pass a structured variable to a function

Post 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())
Good programmers don't comment their code. It was hard to write, should be hard to read.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: How to pass a structured variable to a function

Post 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...
Good programmers don't comment their code. It was hard to write, should be hard to read.
CaddMan
User
User
Posts: 21
Joined: Sat Jul 10, 2004 8:05 pm
Location: USA (East Coast)

Post 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)
Post Reply