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!

Public vs Private?

Status
Not open for further replies.

august

MIS
Aug 24, 2000
150
PH
When where to use Public or Private? what means Public Variables,and Private Variables?

August

[sig][/sig]
 
august

PUBLIC creates memory variables, (or arrays), that can be used or modified anywhere within your application, or in other words, globally. The first character "g" identifies the variable as being global.

PUBLIC gcAlias && Create public variable
gcAlias = ALIAS() && Assign value

PRIVATE hides memory variables, (or arrays), previously declared, from the current program. You cannot use PRIVATE to declare a variable or array

PRIVATE gcAlias
gcAlias = ALIAS() && Assign value

This would convert the variable to being private. Once the program/event/method/procedure containing "PRIVATE" has executed, gcAlias reverts to being public, but the value will be .F.

IMHO, avoid PUBLIC variables if possible, and instead use form properties.
Assuming you have added a form property called NewProperty to your form, then you could:-

DO FORM mainform NAME oMain LINKED

To access NewProperty from the form:-

THISFORM.NewProperty = [Hello World]

To access NewProperty elsewhere in your application:-

oMain.NewProperty = [Hello World]

Hope this helps

Chris [sig][/sig]
 
Chris pretty descibed what they mean how they are used but
I try and keep the use of public variables to a minimum. Everyone uses them occasionaly because they are generally a quick solution to some problem. In large systems, public variables tend to be confusing when your debugging or modifing the system later (Your looking at some snippet in a procedure and you have no idea where this variable is coming from or what it's value may be). The more your systems are OOP'd the less you will see a need for Public variables. At least that is the way it is for me.

As for Private (Perhaps I have missed something) I don't think I have ever explicitly declared a variable to be private. Pretty much any variables you declare in procedures, functions, classes, froms, prgs etc are private (I think it's called "Variable Scoping"/)

[sig]<p> Pete<br><a href=mailto:blindpete@mail.com>blindpete@mail.com</a><br><a href= > </a><br>What your mother told you is true! You will go blind! (from moonshine anyway)[/sig]
 
As for Private (Perhaps I have missed something) I don't think I have ever explicitly declared a variable to be private. Pretty much any variables you declare in procedures, functions, classes, froms, prgs etc are private

Pete, you are correct that, for practical purposes, variables that you create in a method, procedure, etc. act like private memvars, with one difference that may be crucial to an application: the PRIVATE declaration hides memvars that already exist. Examples:

Example 1
cMyVar = &quot;Fred&quot;
do SubRoutine
? cMyVar && returns &quot;Ethel&quot;

procedure SubRoutine
cMyVar = &quot;Ethel&quot;


Example 2
cMyVar = &quot;Fred&quot;
do SubRoutine
? cMyVar && returns &quot;Fred&quot;

procedure SubRoutine
private cMyVar && hides existing cMyVar
cMyVar = &quot;Ethel&quot;


As you can see in the second green example, the child program (SubRoutine) declares the memvar private, thus protecting any value a variable of the same name may have in any parent program.

This can be very important in bullet-proofing and encapsulating your code, and making it more portable. If a memvar name like &quot;lRetVal&quot; is used in many routines in your system, one function that called on another function could get its memvar values changed unintentionally. [sig]<p>Robert Bradley<br><a href=mailto: > </a><br><a href= - Visual FoxPro Development</a><br> [/sig]
 
Robert, that is why I don't like private variables and declare all variables as 'local'. [sig]<p>Vlad Grynchyshyn<br><a href=mailto:vgryn@softserve.lviv.ua>vgryn@softserve.lviv.ua</a><br>[/sig]
 
Another suggestion is to declare all of the public variables in a program file. Therefore it is much easier to track down the global variables. So, in the main.prg we can do something like

DO glabal_variable_file.

Yue Jeen [sig][/sig]
 
Foxdev wrote:
This can be very important in bullet-proofing and encapsulating your code, and making it more portable.


I see your point clearly. I suppose I have been lucky to date. [sig]<p> Pete<br><a href=mailto:blindpete@mail.com>blindpete@mail.com</a><br><a href= > </a><br>What your mother told you is true! You will go blind! (from moonshine anyway)[/sig]
 
Private acts pretty much like a Public Var when Declared at the top level anyways :p [sig]<p>John Durbin<br><a href=mailto: john@johndurbin.com> john@johndurbin.com</a><br><a href= </a><br>ICQ #9466492<br>
ICQ VFP ActiveList #73897253[/sig]
 
Thanks to all of you guys!

August [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top