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:-
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 = "Fred"
do SubRoutine
? cMyVar && returns "Ethel"
procedure SubRoutine
cMyVar = "Ethel"
Example 2
cMyVar = "Fred"
do SubRoutine
? cMyVar && returns "Fred"
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 "lRetVal" 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=
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
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 [sig]<p>John Durbin<br><a href=mailto: john@johndurbin.com> john@johndurbin.com</a><br><a href=
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.