Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Public and local variable of the same name?
Message
From
18/01/2008 16:54:10
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01282432
Message ID:
01283130
Views:
18
>>BTW, it is not necessary to define a global variable as PUBLIC, a PRIVATE defined in the highest program layer will be global to the application. The only thing PUBLIC does is make the variable live beyond the end of the program that defines it.
>
>Thank you for clarifying. Are you saying that PUBLIC variables stay in the memory of the PC even after you close the application?

Function A calls Function B, which in turn calls Function C. Now, you need to use a variable in Function B.

  • If you don't declare it, it may overwrite a variable from Function A. Otherwise, it will work just like a private variable, i.e., it will be accesible in Function C.
  • If you declare it as Local, it is only accesible in Function B. In Function C, it is hidden.
  • If you declare it as Private, it will hide the same variable from Function A (if there is one), but otherwise work like an undeclared variable: it will be accesible from Function C.
  • If you declare it as Public, it will persist in memory even after Function B finishes. This includes Function A, but also the Command Window after Function A closes.

    So, declaring a variable as Public at the top-most level (Function A in the above example) is exactly the same as declaring it Private, or not declaring it at all, except for the visibility at the Command Window (once the main procedure closes). Public variables will persist after the main program closes.

    And, while we are on the subject of variable scope, if you need to share information between different form methods (or methods of an object), usually the best place to put the information is in a form (or object) property. This information will be accesible from all methods of the form, but unlike a public variable, only from the form. If you have two instances of the form, each one has its own, private, copy of the information. - I like to mention this, because I have seen cases of public variables being used, when form properties were more appropriate.
    Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
  • Previous
    Next
    Reply
    Map
    View

    Click here to load this message in the networking platform