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

Scope Issues

Status
Not open for further replies.

SteveMacPSU

Programmer
Sep 8, 2005
32
US
I'm having a real problem understanding scope within a form, method, whatever. I want to populate several different arrays with certain records from a code table and have them available for different combo boxes on different forms. When, where, and how should I create these arrays so that they are available on the specific form when I need them?
 
Hi Steve,

You need to make the arrays into custom properties of the form.

To do so:

1. In the form designer, go to the Form menu, then New Property.

2. In the New Property dialogue, type a name for the array, and add a number in square brackets. For example: MyArray[1]. It doesn't matter what the number is for now. It's the presence of the square brackets that make this into an array.

3. Click Add. Add further arrays in the same way. Finally, click Finish.

4. In your code, reference the array by preceding it with THISFORM..

For example, to use one of these arrays as the row source for a combo box:

this.RowSource = "THISFORM.MyArray"

(Note the quotes in the above example.)

I hope this will give you a good start.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Thanks Mike, that makes sense. I just couldn't for the life of me figure out how to get that done. Would that be the same technique to use for declaring variables I may need throughout my form? I have vowed to get away from declaring public variables. Your continued help is greatly appreciated.
 
Yes, you can add properties to the form and then they can be referenced everywhere.

You can also use AddProperty method to do this programmaticaly, e.g.

thisform.AddProperty('cCustomProperty','')
 
A rethink: If using VFP8/9 (I think this was introduced in 8, but I am not sure)
If the codes are used by the complete application, rather than arrays, Public variables etc, use Scatter Name <<object>>. But the codes have to be in a Single record. (Multiple columns). You can also have multiple tables but can use a single record only.
Example:
Public oCodeObject as object
Select <<code1>>
Scatter name oCodeObject
Select <<code2>>
Scatter name oCodeObject additive
Now this object is available throughout the app. You access the values by
oCodeobject.<<field name1,Field name2 …>>
if you want to update the tables with changed values
oCodeObject.name = “Steve”
select the correct table where “name” came from;
gather name oCodeObject

Though a little planning is required, cannot have the same names in the table you are selecting, as the second table variable will over write the fist one
Look up Scatter Name in help.
 
Steve,

Glad it made sense.

You wrote:
Would that be the same technique to use for declaring variables I may need throughout my form?

Yes, that's correct. Use a form property for any variable (whether an array or a simple value) that you want to access from any code in the form, but not from outside the form. In other words, a form property is scoped to the form.

If you only want to reference a variable within a single method, use a local variable. To do so, add a LOCAL statement to the start of the method.

If you want to scope a variable to the entire application, use a public variable. Some developers frown on these, and others are dogmatically against them. My own view is that public variables are occasionally useful, but you use them sparingly.

Hope this makes sense.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
I don't really agree with everything here. Global variables, in general, are considered bad in OOP. Perhaps a rethink of what you need and where you need it and then how to get it would make sense.

Craig Berntson
MCSD, Visual FoxPro MVP, Author, CrysDev: A Developer's Guide to Integrating Crystal Reports&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top