TIP: Click on subject to list as thread! ANSI
echo: power_bas
to: JIM TANNER
from: DAVE NAVARRO
date: 1995-06-05 15:14:00
subject: Byval

JT>    Please post some guidelines for using BYVAL when passing a STRING 
variab
JT>(when to use it /// when not to use it).
Hmm..
Well, I really don't have any guidelines.  But I can tell you what
happens:
** Passing Strings BYVAL to a SUB or FUNCTION **
- Passing a dynamic string:  PowerBASIC makes a copy of the dynamic
  string and pushes the handle on the stack.  If your SUB or FUNCTION is
  written in BASIC, then PowerBASIC releases the temporary string when
  the SUB/FUNCTION ends.  If your SUB or FUNCTION is written in
  assembler or C (the built-in assembler does not count) then your SUB
  or FUNCTION is responsible for releasing the temporary string before
  control is returned to PowerBASIC.
- Passing a flex string:  PowerBASIC makes a copy of the flex string and
  converts its type to a dynamic string.  Same rules as above.
- Passing a fixed length string:  PowerBASIC creates a dynamic string
  and copies the data from the fixed length string into it.  Save rules
  as above.
If you are passing a dynamic string to a SUB/FUNCTION which expects a
dynamic string BYVAL and your SUB/FUNCTION never modifies the string
then this is very inefficient.
However, *I* prefer to pass strings BYVAL despite the above inefficiency
with DYNAMIC strings because PowerBASIC will "convert" a string of
another type into a dynamic string.  This means I don't have to worry
about it myself.
Example:
SUB ChangeDrive( NewDrive$ )
  CHDRIVE NewDrive$
END SUB
DIM d1 AS STRING * 2
MAP d2$$ * 2
DIM d3 AS STRING
d1   = "A:"
d2$$ = "A:"
d3   = "A:"
ChangeDrive d1      'type mismatch error generated
ChangeDrive d2$$    'type mismatch error generated
ChangeDrive d3      'works because types match
---
SUB ChangeDrive( BYVAL NewDrive$ )
  CHDRIVE NewDrive$
END SUB
DIM d1 AS STRING * 2
MAP d2$$ * 2
DIM d3 AS STRING
d1   = "A:"
d2$$ = "A:"
d3   = "A:"
ChangeDrive d1      'works:  converted to dynamic string
ChangeDrive d2$$    'works:  converted to dynamic string
ChangeDrive d3      'works:  copied to temporary string (inefficient)
Hope this info helps.
--Dave
---
 þ QMPro 1.53 þ People say I'm indecisive.  Am I?  I don't know.
--- QScan v1.065b
---------------
* Origin: Nitelog BBS Monterey CA (408) 655-1096 (1:216/303) (1:216/303)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.