Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Scope Across A Form

Status
Not open for further replies.

IForgot

Programmer
Mar 20, 2002
122
0
0
US
As a former FPW application developer I have gotten used to being able to count on variable values across the whole host of Screen/Form snippetts via Scope as long as the variables were defined in the Startup snippett or earlier in a calling program.

Now that I am working in VFP 6 & 7 I am getting frustrated by finding variables defined within the Load or Init Method not existant and/or not available in other subsequent methods unless I specifically declare them PUBLIC.

If I have to live with this annoyance, what is the best way to handle it?

If there is someway in which I can eliminate this annoyance, I would appreciate learning how.

Thank you,
I_Forgot
 
Instead of a variable, use a custom property on the form. You are correct in not wanting to use PUBLIC variables...IMO, they should be avoided if at all possible.

Craig Berntson
MCSD, Visual FoxPro MVP, Author, CrysDev: A Developer's Guide to Integrating Crystal Reports"
 
If the variables are defined in a startup .prg they will be available throughout. -Jim
 
Another suggestion is,

If you need a variables that will be use by more than one form, create a startup program (MAIN.PRG). Then you can declare it

PRIVATE myVar

-- AirCon --
 
jimstarr,

You beat me. Arrggghhh..I'm not a fast typewriter :-D

-- AirCon --
 
JimStarr & AirCon -- The "define varables in PRG prior to launching the Form" methodology is consistent with my old FPW programming methods. It does indeed work and I do use it sometimes.

However this approach is a "hold-over" from the pre-Visual FP days and I would like to discover new ways of getting the job done and do so by getting "VFP-aligned".

Craig -- That sounds like a good "VFP/Object Property" approach which I had not thought of before. Thanks - I'll try it out.

Thanks to all.
I_Forgot
 
I like Craig’s suggestion the best. If the variable (now a property on the form) is going to be used in multiple spots, you can add it to your session variable if you use something like this in startup:

set classlib to myclass
PUBLIC goApp
goApp = CREATEOBJECT('cApp')

In this instance, you should add the property to the container cApp in your classlib and then the session variable "goApp" will contain that property globally.

So rather than having a bunch of public variables, your common items can be stored as properties on capp. Examples:

goApp.username
goApp.userID
goApp.dataDir

I like this approach. However, there is a little overhead because the container object is not light. It contains a lot of properties, etc. that you will not be using in this case. In VFP 8.0, you now have the ability to create an "empty" class, and you have the ability to create properties on the fly with the new addproperty function.

Therefore, a lighter fit would be using this method. Here is a sample in code:

goApp = CREATEOBJECT('empty')
ADDPROPERTY(goApp,"username")
goApp.username = "Sammy Sample"
? goApp.userNAME

Go ahead and try it if you have VFP 8.




Jim Osieczonek
Delta Business Group, LLC
 
Is there a size limitation to the name of a parameter that can be added to an empty class?

I have been changing over my public variables to the empty object this thread described which is working great for me but I am erroring out when trying to create the parameter gcBankingTableDirectory.

Any assistance would be greatly appreciated.

Michael
 
I believe that the maximum length of a variable and/or a property is 128 characters. I successfully added 30 more characters to your variable name and it seems just fine. What sort of error are you getting and when?

Rick
 
I think I resolved the issue but I'm not sure that I understand why it worked.

If the first Parameter Name is the longest name of all the parameters, it works seamlessly. When I created in this order:
gcUserTable
gcBankingTableDirectory
it blew up creating the gcBankingTableDirectory

SO as long as I make the first Parameter a very long name I am all set.

BTW, I discovered (thanks intellisense) that the add property argument can also have a value for the parameter.

AddProperty(objectname, ParameterName, ParameterValue)

Thanks,

Michael
 
It turns out that I need to re-visit this issue once more.

As I have mentioned above, I have been using VFP7 and its Form Designer to build forms.

When I add a property, as suggested above, to the form via the Form Designer, it does indeed add a new property, but that property always shows up as a Logical property.

How can I add new Properties to a Form to be used as "variables" across the various methods of the form so that the new Property will hold values other that Logical such as Date, Numerical, Character, etc.

Your help and advice is greatly appreciated.

Thanks,
I_Forgot
 
In the Property Sheet, set the Form Variable value for:
Character: ="StringEnclosedInQuotes".
Note the equal sign and quotes.
Number : Just the number.
Date : { / / }.
The last two do not have quotes.

Andy Rice
San Diego, CA
 
In addtion, you can add a property as an array. I do this sometimes when the array is unique to the form, or at the class level if the array is shared.

Form a from.

New Property.
arMyArray[1]

It can be dimensioned later, let's say in the init event.

DIMENSION THISFORM.arMyarray[3]
THISFORM.arMyarray[1] = 'Red'
THISFORM.arMyarray[2] = 'Green'
THISFORM.arMyarray[3] = 'Blue'

Jim Osieczonek
Delta Business Group, LLC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top