Re: VB6 and global variables

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Gary German (garyg_at_NOSPAMXshastasoftware.com)
Date: 04/12/04


Date: Mon, 12 Apr 2004 13:18:40 -0700


"Pete" <anonymous@discussions.microsoft.com> wrote in message
news:60F7051F-537A-4DE8-B4CE-C14309B42B3B@microsoft.com...
> Hi All,
>
> Let me start by saying I know it is a bad programming practice.....
>
> If you have a GLOBAL variable (in a bas module) and you have a form and
inside of that form you have a variable that uses the same name as the
GLOBAL, how do you access the GLOBAL and not the routines version of the
variable?
>
>
> ** This is in VB6 sp3.
>
> Thanks for your help

It's a little-known fact that you can use the module name, and dot syntax
together. So, if you have a module named "Globals", and a public variable
in the module named "Foo", you can refer to it in your code as Globals.Foo.

That said, this is not usually a good idea. Most shops use coding standards
that emphasize scope as part of the name. I use an "m" prefix to designate
module-level scope, and a "g" prefix to indicate global-level scope. So,
using the example above, my global scoped module would be named something
like "gsFoo" (to indicate a global string variable", or "msFoo" to indicate
a module level string variable.

BTW - why are you using SP3? SP5 has been out for a long time now, and SP6
was recently released (but, many are urging caution before applying SP6).

GG
http://www.shastasoftware.com - Shasta Software - Consulting, Design, and
Programming Services
http://www.CycliStats.com - CycliStats - Software for Cyclists
http://www.StrideWare.com - StrideWare - Software for Runners and Walkers



Relevant Pages

  • Re: Classes / Functions / Autonomy
    ... One thing I do realize after toying with OO programming for a while now: ... and they will never interfere with the rest of the script. ... Of course when I first started out, I was using globals. ... Constants are freely available in functions without explicitly making them ...
    (comp.lang.php)
  • Re: global/local variables
    ... Python "goto" module, and use goto and comefrom in your code. ... Globals aren't as harmful as goto. ... programming at all. ...
    (comp.lang.python)
  • Re: global variables are bad?
    ... placed in a file of their own, and the globals should not have external ... This means putting the declaration in the header file, ... extern int shared_int; ... sort of thing crops up a lot (I don't do embedded programming, ...
    (comp.lang.c)
  • Re: Global Variables
    ... heavily on global variables. ... All the state machines have access to all the settings and status ... I really don't see a better way than this - globals and all - although ... programming paradigm, rather than the more usual C imperative ...
    (comp.arch.embedded)
  • Re: Permanently "global" ?
    ... > language and run back home to perl. ... you need to rethink your programming style if your functions/subs ... have to access more than a handful of globals. ... functions that communicate with each other through global variables, ...
    (comp.lang.php)