INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

TEXT...ENDTEXT updates a hidden variable instead of creating private variable
3

TEXT...ENDTEXT updates a hidden variable instead of creating private variable

TEXT...ENDTEXT updates a hidden variable instead of creating private variable

2
(OP)
Ran into this strange bug today. Seems to be a lesson in how VFP scoping can be weird, and why you should always initialize variables. Code is below.

CODE

CLEAR

*
* [1] Initialize pcVar, which is PRIVATE by default
*
pcVar = PROGRAM()
DISPLAY MEMORY LIKE pcVar

TestProc()

*
* [5] pcVar updated to 'TESTPROC'
*
DISPLAY MEMORY LIKE pcVar

RETURN

******************
PROCEDURE TestProc
******************
	*
	* [2] Hide pcVar from this procedure
	*
	PRIVATE pcVar
	DISPLAY MEMORY LIKE pcVar
	
	*
	* [3] Write to pcVar, which *should* be a new variable, private *to this procedure* by default
	*
	TEXT TO pcVar NOSHOW TEXTMERGE
		<<PROGRAM()>>
	ENDTEXT
	
	*
	* [4] TEXT...ENDTEXT just updated a hidden variable
	* Note: if pcVar were PUBLIC in the calling procedure, it would still get updated
	* 
	* Initializing pcVar after the PRIVATE command, but before the TEXT...ENDTEXT block would completely avoid this error
	*
	DISPLAY MEMORY LIKE pcVar
	
	RETURN
ENDPROC
******* 

Output, annotated:

CODE -->

[1] PCVAR             Priv      C   "00002Q6H01J7"  00002q6h01j7
[2] PCVAR             (hid)     C   "00002Q6H01J7"  00002q6h01j7
[4] PCVAR             (hid)     C   "               TESTPROC"  00002q6h01j7
[5] PCVAR             Priv      C   "               TESTPROC"  00002q6h01j7 

Essentially, though I hide pcVar from TestProc() with PRIVATE, TEXT TO pcVar sets the hidden, out of scope pcVar instead of creating a new private variable scoped within TestProc(). Interestingly, if there are multiple hidden variables at the same time, TEXT TO will set the closest variable in the callstack.

This isn't specific to out-of-scope PRIVATE variables: TEXT..ENDTEXT will also update out-of-scope PUBLIC variables.

This wouldn't be an issue if I had instantiated pcVar within TestProc() between the PRIVATE and TEXT TO command, which serves as a lesson to always instantiate your variables.

RE: TEXT...ENDTEXT updates a hidden variable instead of creating private variable

I can confirm, this even is in the last VP9 SP2 hotfix and the workaround is initalizing the variable.

The promise of the TEXT...EMDTEXT help topic is:

Quote (VFP help)

If the variable has not yet been declared, Visual FoxPro automatically creates it as a private variable.
This is not done.

To illustrate this better you may change the line PRIVATE pcVar within the TextProc procedure to PRIVATE ALL

That will make clearer the PRIVATE command is no variable declaration command like LOCAL. That still doesn't explain it, the promise of the help topic is not kept.

Here's an example showing how ASTACKINFO behaves correctly, the bug really is with TEXT..ENDTEXT, not with private variable scoping in general:

CODE

CLEAR

*
* [1] Initialize paVar, which is PRIVATE by default
*
? "[1] paVar initialized:"
ASTACKINFO(paVar)
DISPLAY MEMORY LIKE paVar

TestProc()

*
* [4] paVar uncganged by 'TESTPROC'
*
? "[4] paVar unhidden, unchanged by Testproc and exactly as initialized in [1]:"
DISPLAY MEMORY LIKE paVar

RETURN

******************
PROCEDURE TestProc
******************
   *
   * [2] Hide paVar from this procedure
   *
   PRIVATE ALL
   ? "[2] Show the incoming paVar is hidden:"
   DISPLAY MEMORY LIKE paVar
   
   * Set paVar, which will be a new variable, private *to this procedure* by default
   ASTACKINFO(paVar)
   
   *
   * [3] Show ASTACKINFO behaves correctly
   *
   ? "[3] Show the recreated internal paVar within TestProc (this section has paVar twice, once hidden (hid), once Private):"
   DISPLAY MEMORY LIKE paVar
   
   RETURN
ENDPROC
******* 

Thanks for the heads up.

Bye, Olaf.

RE: TEXT...ENDTEXT updates a hidden variable instead of creating private variable

(OP)
Thanks for the confirmation and help, Olaf.

Glad to know the bug only occurs for TEXT..ENDTEXT, and doesn't appear to be an issue with other functions.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close