News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Trouble passing one dimensional Array Type to Function.

Started by SpellSword, January 11, 2008, 04:12:54 AM

Previous topic - Next topic

SpellSword

I've run into a problem trying to pass a one dimensional Array Type into a Function. When I try, I get this error:
Compile Time Error:
Line: 44
Test
Error Number: 401
Error String: 'test' Parameter (#1) is incorrect Type, Expecting Integer
Array Handle Data type.


I've haven't tried passing Arrays that have been declared as a Type into a Function before, so I'm not sure exactly what my mistake is.  ???

Here's an example that produces the error:
;
;
;
;

REM THE TYPE
Type DT
YS
D$
EndType

REM Creating Array as DT Type
DIM ZData(6) AS DT

REM Data
; 01
ZData(1).YS = 1
ZData(1).D$ = "One"

; 02
ZData(2).YS = 3
ZData(2).D$ = "Two"

; 03
ZData(3).YS = 3
ZData(3).D$ = "Three"

; 04
ZData(4).YS = 3
ZData(4).D$ = "Four"

; 05
ZData(5).YS = 3
ZData(5).D$ = "Five"

; 06
ZData(6).YS = 3
ZData(6).D$ = "Six"

REM Call Function
Test(ZData())

REM End Program
END

Rem Function
Function Test(ZData())
Print ZData(6).YS$
Sync
Waitnokey
Waitkey
EndFunction
When I dream,
I carry a sword in one hand,
a gun in the other...

kevin


try this.


REM THE TYPE
Type DT
YS
D$
EndType

REM Creating Array as DT Type
DIM ZData(6) AS DT

REM Data
; 01
ZData(1).YS = 1
ZData(1).D$ = "One"

; 02
ZData(2).YS = 3
ZData(2).D$ = "Two"

; 03
ZData(3).YS = 3
ZData(3).D$ = "Three"

; 04
ZData(4).YS = 3
ZData(4).D$ = "Four"

; 05
ZData(5).YS = 3
ZData(5).D$ = "Five"

; 06
ZData(6).YS = 3
ZData(6).D$ = "Six"

REM Call Function
Test(ZData())



REM End Program
END

Rem Function
Function Test( me().dt)    ;  If the array is a type, them this parameter needs to be declared as such

Print me(6).YS$
Print me(6).d$

Sync
Waitnokey
Waitkey
EndFunction


SpellSword

Huzzah! That worked.
Thanks for the help!
When I dream,
I carry a sword in one hand,
a gun in the other...

andrik

Hi,

Continuing the example, suppose you want to edit the values inside a function? Do you have to return the array as output parameter?

best regards,
Andrik

kevin

 No, the pass is by reference. So any changes you make inside the function, will alter the passed structure.



ie.


Dim Table(10)
Dim Stuff(20)

; fill table with random values
FillArray(Table())
FillArray(Stuff())

ShowArray(Table())
ShowArray(Stuff())

Sync
waitkey


Function FillArray(me())
For lp=0 to getArrayElements(Me(),1)
me(lp)=rnd(1000)
next
EndFunction


Function ShowArray(me())
For lp=0 to getArrayElements(Me(),1)
s$=s$+str$(me(lp))+","
next
print trimright$(s$,",")
EndFunction