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!

EVERY VFP FOLKS MUST READ IT (a Better Form's Object refrence) ?

Status
Not open for further replies.

stvhui

Programmer
Jul 17, 2002
13
0
0
ID
Hello to you,

I'm using vfp7 sp1

The big problem in vfp programming is How to makes the form's retain/remain in a memory ?

I meant that in general "visual programming language" like VB, DELPHI, POWERBUILDER, VISUALDBASE (that's all I'd tested), When loading a form and the form becomes remain in the memory.

But not in VFP, we must make a way to makes the form remain in the memory.

e.g We are opening 3 forms (FORM_A, FORM_B, FORM_C)
The simple as I know is :

PUBLIC FORM_A
DO FORM FORM_A NAME FORM_A LINKED NOSHOW
FORM_A.SHOW


Why ?
Because Just in this way I can access all the contains in form.

Let say, all I opened form is in MODELESS
As we know VFP is based on "multiple instance of form" that means, If we call the above code two times that we can have two form in our screen. not like in VB,DELPHI,POWERBUILDER.

so How to protect it ?
I usually do :

PUBLIC FORM_A
IF VARTYPE( FORM_A ) != "O" && Letter of O not Zero
DO FORM FORM_A NAME FORM_A LINKED NOSHOW
ENDIF
FORM_A.SHOW

I usually put this code in my menu click.

Based on my experience in VFP programming, I'd some sugestions :
- Never Deal with FORM'S DATAENVIRONMENT
- Set Every Form's DATASESSION TO PRIVATE

Based on above so in my form's LOAD always had this code :
SET DATE ITALIAN
SET ...
SET ...

USE .. IN 0 && Zero
USE .. IN 0 && Zero

From the above code you also can "FORM'S TALK" (Communication from One form to another form even VFP not have ability to passing the parameter)

BUT...
I NEVER RELEASE THE PUBLIC VARIABLE
Because I have no idea how makes it release in generic way.

I share this experience is because I wanna Is there another better way to keep the form's object refrence ?
How the VFP programmers do in this situations ?

Please Advice
Steven
 
not sure if i understand, but forms nave a way to pass parameters.

also forms can be loaded in memory without being visible.

and all forms can be referenced with their propertys and methods called. Attitude is Everything
 
Steven,
Once you get past the early learning stage, you'll likely decide you need a framework for your VFP applications. Whether you use a commercial one or develop your own, will usually depend on your needs, budget and experience. Almost all frameworks use an application object, and one of the main components in it, is a Form manager. There are as many ways to "manage" forms as there are frameworks, but most use an array to keep track of the current active forms and all the properties required for the framework.

For some comparisons on frameworks, go to and the comments at You may also be interested in the discussion at
Rick
 
Steven

Just a couple of comments about the code you are using

PUBLIC FORM_A
DO FORM FORM_A NAME FORM_A LINKED NOSHOW
FORM_A.SHOW

could and should be :-

PUBLIC FORM_A
DO FORM FORM_A NAME FORM_A LINKED

and

PUBLIC FORM_A
IF VARTYPE( FORM_A ) != "O"
DO FORM FORM_A NAME FORM_A LINKED NOSHOW
ENDIF
FORM_A.SHOW

should be :-

IF VARTYPE( FORM_A ) != "O"
[tab]PUBLIC FORM_A
[tab]DO FORM FORM_A NAME FORM_A LINKED
ELSE
[tab]FORM_A.SHOW
ENDIF

You are unnecessarily declaring FORM_A to be a PUBLIC variable, while it already exists as one.

You should also try your code without the PUBLIC variable declaration - if it works correctly, then leave it out.

PUBLIC variables should only be used as a last resort, if, as and when all else fails.

You are not differentiating between the form name and the object name, so try prefixing the name with 'o'.

Finally, to release the PUBLIC variable for each form, put :-

RELEASE oFORM_A

in the .Unload() event of the relevant form.

Close the form - that will release the PUBLIC variable, it's no more complex than that. HTH

Chris [pc2]
 
I created PUBLIC var is usefull when your opening some forms. I meant your are opening 3 forms and the form_A is an ACTIVEFORM
 
stvhui

Try commenting out the PUBLIC variable references, save compile, and run your project.

You will quickly find out if you need to declare the variables as PUBLIC as you will get an error message if the variables are out of scope, or in other words, can't be seen by all the objects.

Just to further explain about :-

"I NEVER RELEASE THE PUBLIC VARIABLE
Because I have no idea how makes it release in generic way."

It is important to release the form's object reference in the .Unload() event of the form, which is also answering your question above.

You can also release another form and the PUBLIC variable at the same time with :-

IF VARTYPE(oForm_B) = [O]
[tab]RELEASE oForm_B
ENDI

If you don't release the object reference, the object remains in memory, taking up resources and slowing your application down. This is also referred to as a 'dangling' object reference. HTH

Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top