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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public variables

Status
Not open for further replies.

phita

Programmer
Jun 8, 2001
82
KE
Hi everyone,
How do you declare a variable in a VFP 6.0 form so that it is acceble in all the methods(procedures) in a form ? Currently, I am declaring the variables as PUBLIC in the Init event of the form but I know this is dangerous in the long run but it was my only way out. I know in Foxpro 2.6 I declare the variables in the Screen Setup Code and these variables are accessible in any procedure of the Screen.

Someone please help. I might end up with 1,000 PUBLIC variables in the long run.

Thanx,

Phita.
 
Use properties instead. Properties are accessible in the entire form and go out of scope (logically) with the form.

just define a property at design time and you can use it in the entire form (same goes for methods).

When in design mode, choose menu option class\"edit method\properties"
or Form\"edit method\properties".

HTH,

Weedz (Wietze Veld) They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best. - Steve McConnell
 
To add a form property that can be "seen" by all the code in a form:
1) Open the form
2) From the Menu choose Form -> New Property
3) Supply the name and click Add.

Or
2a) From the Menu choose Form -> Edit Property/Method
2b) Click on New Property

You can then refer to it as:
ThisForm.mynewpropery
in your code.

Note: If you want to add an Array, just make the name something like:
MyArray[1]

Then in your Load() or Init() method, you can resize it to the proper size - e.g.:
DIMENSION ThisForm.MyArray[3,3]

Rick

 
In addition to my previous message, if you decide to use public variables, I suggest to use one public object (which means only one public variable), and add properties to this public object so that you don't have to declare all those untracable variables.

HTH,
Weedz (Wietze Veld) They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best. - Steve McConnell
 
Thanx for the replies. Now, one more question. What if I want to be able to access the variables from a report that I run from within the form ? Weedz suggested a public object. It sound a good idea. So weedz, can you please furnish me with more details on how to create a public object and how to access its properties from other forms.

Thanx,

phita.
 
You will have to create a class based on f.i. a custom class and add properties to it. Store it in f.i. MyClass.VCX

then issue the following code:
(Create in your main program your public object)
PUBLIC oObject

SET CLASlIB TO MyClass.VCX
oObject = CREATEOBJECT('cMyPublicVarClass')

HTH,

Weedz (Wietze Veld) They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best. - Steve McConnell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top