Re: VB6 Scope Issues
- From: argusy <argusy@xxxxxxxxxxxxxxx>
- Date: Mon, 24 Jul 2006 14:50:21 +0930
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:Can you explain the last sentence? It is not clear what the "it" refers to.
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.
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
.
- References:
- VB6 Scope Issues
- From: Andrew Chalk
- Re: VB6 Scope Issues
- From: Bob O`Bob
- Re: VB6 Scope Issues
- From: Andrew Chalk
- Re: VB6 Scope Issues
- From: Bob O`Bob
- VB6 Scope Issues
- Prev by Date: Re: I've lost my messagebeep!
- Next by Date: Re: I've lost my messagebeep!
- Previous by thread: Re: VB6 Scope Issues
- Next by thread: Re: VB6 Scope Issues
- Index(es):
Relevant Pages
|