Page 1 of 1

Calling Procedures With Structure Arguments

Posted: Fri Mar 07, 2003 2:46 am
by BackupUser
Code updated for 5.20+

Restored from previous forum. Originally posted by Inner.

Code: Select all

#MAX_VALUE=3

Structure TESTSTRUCT
  name.s
  number.l
  array_string.s[#MAX_VALUE]
  array_long.l[#MAX_VALUE]
  array_word.w[#MAX_VALUE]
  array_byte.b[#MAX_VALUE]
EndStructure

mystruct.TESTSTRUCT

mystruct\name="Test String"
mystruct\number=3665412
For i=0 To #MAX_VALUE-1 : mystruct\array_string[i]="String Array "+Str(i) : Next
For i=0 To #MAX_VALUE-1 : mystruct\array_long[i]=Random(100) : Next
For i=0 To #MAX_VALUE-1 : mystruct\array_word[i]=Random(100) : Next
For i=0 To #MAX_VALUE-1 : mystruct\array_byte[i]=Random(100) : Next

Procedure Display_TESTSTRUCT(*outstruct.TESTSTRUCT)
  Debug(*outstruct\name)
  Debug(*outstruct\number)
  For i=0 To #MAX_VALUE-1 : Debug(*outstruct\array_string[i]) : Next
  For i=0 To #MAX_VALUE-1 : Debug(".L Array "+Str(*outstruct\array_long[i])) : Next
  For i=0 To #MAX_VALUE-1 : Debug(".W Array "+Str(*outstruct\array_word[i])) : Next
  For i=0 To #MAX_VALUE-1 : Debug(".B Array "+Str(*outstruct\array_byte[i])) : Next
EndProcedure

Display_TESTSTRUCT(@mystruct)

Posted: Fri Mar 07, 2003 5:10 pm
by BackupUser
Restored from previous forum. Originally posted by Franco.

Keep in mind, you don't copy the values from mystruct to outstruct.
You only pass the pointer to the procedure, mystruct and outstruct share the same pointer.
If you change the values in the mystruct structure also the values getting from outstruct are changed!

If you need access to a structure inside a procedure you have to use the shared command:

Code: Select all

Structure TESTSTRUCT
    name.s
    number.l
EndStructure

mystruct.TESTSTRUCT

mystruct\name="Test String"
mystruct\number=3665412

Procedure WhatEver()
  Shared mystruct
  Debug(mystruct\name)
  Debug(mystruct\number)
EndProcedure

Have a nice day...

Franco

Posted: Fri Mar 07, 2003 6:50 pm
by BackupUser
Restored from previous forum. Originally posted by freak.

@Franco,
The shared Command actually does exactly the same, if you now change some Value in the Procedure, it is also changed in the main Prog, because now it is not only the same pointer, but also the same variable :)

What you said was right though...

Timo

Posted: Fri Mar 07, 2003 7:34 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.

Does this qualify as a tip or a trick? Haven't we all been doing this for a long time?

No offence meant to Inner.


--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.51, Ed3.53)

Posted: Sat Mar 08, 2003 2:56 am
by BackupUser
Restored from previous forum. Originally posted by blueb.

Question:

How about:

Code: Select all

 Global mystruct.TESTSTRUCT
Would that work as well, that way you wouldn't worry about sharing variables in procedures?

--blueb