Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calling Procedure in a Program (.PRG)
Message
From
23/10/2018 14:45:16
 
 
To
23/10/2018 14:00:13
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows XP
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01662746
Message ID:
01662748
Views:
38
>Respected all,
>
>I have a .prg File , in which I have sequence of procedures I have to carry on one by one to get my desired results. Till Now I have kept all procedures in sequence of one another, but since there are many such procedure (Steps) involved and my .prg has become big and less understandable easily.
>
>I am thinking to put procedures at the end of .prg and Call that procedure step by step so that it may be easily comprehended.
>
>For this I have kept one of such Procedure under PROCEDURE LEFTBOX at the end of my .prg file and calling that procedure using DO Leftbox.
>
>The procedure is generating value for it but when it comes back to the main routine it gives error as variable Boxstartrow not found.
>
>If I declare Boxstartrow as PUBLIC, it works fine.
>
>Will I have to declare all such variables as Public, which I am intending to use in procedure or there is some other way also ?
>
>Here is my Code
>
>
>Do Leftbox
>
>Do abc
>
>Do xyz
>
>*Some Other Code
>
>PROCEDURE Leftbox
>Locate For Left(Alltrim(PS),4)='1 w1' AND Right(Alltrim(PS),1)='k'
>Replace PS With ;
>STRTRAN(PS,Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-1), Alltrim(Str(Val(Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-1))-15))) ;
>PS With ;
>STRTRAN(PS,Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-4), Alltrim(Str(Val(Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-4))-4)))	;
>PS With ;		
>STRTRAN(PS,Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-2), Alltrim(Str(Val(Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-2))+4)))
>cNEWBOTTOMLINE=Getwordnum(Alltrim(PS), Getwordcount(Alltrim(PS))-1)
>BOXSTARTCOLUMN=Val(Getwordnum(PS, Getwordcount(PS)-4))
>BOXENDCOLUMN=385
>BOXSTARTROW=Val(Getwordnum(PS, Getwordcount(PS)-3))
>BOXENDROW=BOXSTARTROW+Val(Getwordnum(PS, Getwordcount(PS)-1))
>BOXCOLUMNRANGE=385-m.BOXSTARTCOLUMN					
>RETURN
>
>
>Thanks as Always.

From what you describe, it sounds like that the variable did not exist prior to the call to the procedure. You're probably running into problem which is related to scoping of variables. By default the scoping is PRIVATE - meaning that within the context of a subprogram, if the variable doesn't already exist, a new one is created, and is discarded upon exit. If the said variable does exist, then the pre-existing variable is used. Basically it appears that you're expecting a subprogram to assign variables -- but since this variable didn't exist before you called that subprogram, the variable was created then destroyed in the subprogram's context.
* Let us assume at this point that variable "MyVar" doesn't currently exist.

? VARTYPE(MyVar)
* Above displays "U" since MyVar does not yet exist within this context
DO Proc1
? VARTYPE(MyVar)
* Above displays "U" since MyVar does not exist in this context, when subprogram was called, it created (within its own context) the
* variable and assigned it, then discarded it upon exit.

MyVar = 1  && Variable MyVar is created and assigned a value
? VARTYPE(MyVar)
* Above displays "N"
DO Proc1
? VARTYPE(MyVar)
* Above will display "C" -- since the variable exists within the context of the caller of Proc1, the subprogram will assign the existing variable.


PROCEDURE Proc1
    MyVar = "Test"
ENDPROC
It may be a good idea that variables to be assigned within a subprogram to be passed as parameters -- this would avoid any hidden "side effect".
Previous
Reply
Map
View

Click here to load this message in the networking platform