Re: VB6 Scope Issues

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi, Bob
I agree wholeheartedly with your last paragraph. If I know a variable is going to be used throughout a program, and it is NOT specific to a module operation, I'll declare globally.

For example, a boolean variable to flag a condition

public function <doSomething>
flag = false 'declared globally
<some code, maybe open a recordset ...>
do
<more code...>
if aCondition is true then flag = true
loop
if flag = false then doSomeOtherThing 'weak I know, could use it above
exit sub

I **could** have declared 'flag' as a local boolean variable
(dim flag as boolean)
and it disappears from scope when the function has ended.
or as a form variable
(public flag as boolean)
and then it disappears when the form is unloaded

PROVIDED I declare its condition before I use it, I don't see any problem
(If I didn't, and it has been set elsewhere, it could stuff up the logic within this function - which is a good reason to declare it inside the function, isn't it?. Hmmm. Bad choice, off the top of my head, but it serves the purpose for Andrew's query)

Argusy

Bob O`Bob wrote:
Andrew Chalk wrote:

"Bob O`Bob" <filterbob@xxxxxxxxxxxxxxx> wrote in message news:u2a4wmprGHA.3828@xxxxxxxxxxxxxxxxxxxxxxx

Andrew Chalk wrote:

1) What is the difference in scope of:
Public X As String
Global X As String


Anywhere where the latter is not a syntax error, there is no difference between the two.

Anywhere else (place where you can only use Public) it /is/ actually rather different
from *either* of the above.

Can you explain the last sentence? It is not clear what the "it" refers to.


In a standard or "code" module (file extension "bas") the two lines are
precisely equivalent, and either one actually instantiates the variable.

In a class or form module the Global declaration is illegal and the Public
declaration creates a property which requires a reference to an instance
before it actually means anything at all.


... which is why I've established a /personal/ standard to NEVER use Public
where Global is legal. That way at least in /my/ code each keyword
[pretty much] always means the same thing. Some folks have argued that
the Global keyword is "deprecated" and should be replaced by Public,
but I most emphatically disagree.




Bob

.



Relevant Pages

  • Re: Class Modules - PredeclaredId ?
    ... I would suggest that you avoid using that internal flag. ... in access 97 you were able to declare and create class modules, ... I can't think of any real advantage of being able to use class modules ...
    (microsoft.public.access.modulesdaovba)
  • Re: TIP #193: Simple Syntax Help System
    ... provides a homogeneous way to declare ... For example, the -arguments flag give ... command or method access mode), ... Juan Carlos--- ...
    (comp.lang.tcl)
  • RE: Public variables I guess
    ... commenting out the 'Flag = 0' from my BefUpdt ... Dim myvarOldValue As Variant ... "Constants, fixed-length strings, arrays, user-defined types, and Declare ... i typically don't worry about the public-ity of variables but i think this ...
    (microsoft.public.access.formscoding)
  • RE: Public variables I guess
    ... Dim Flag As Variant ... Dim myvarOldValue As Variant ... Public Const Flag As Variant = 0 ... "Constants, fixed-length strings, arrays, user-defined types, and Declare ...
    (microsoft.public.access.formscoding)
  • Re: Why is "False" = "-1"?
    ... >>> end up with an indeterminate value in a VB Boolean data type -- that ... >> declaring the API call... ... > The situation is when you declare an external function as having type ... > Dim Boole as Boolean ...
    (microsoft.public.vb.general.discussion)