Home All Groups Group Topic Archive Search About

Global (Public) variables

Author
21 Oct 2005 10:14 PM
Sharp
I'm working on an application that I've set up with several forms,
each to perform a particular set of functions. I'd like to have a few
variables that are common to all forms, figuring that is the way to
share the data among forms.
1. How do I make that work?

2. Is there a better approach to having some core data available from
each form?

3. Is there a better way to organize the whole schmear?

Author
21 Oct 2005 10:25 PM
Karl E. Peterson
Sharp wrote:
> I'm working on an application that I've set up with several forms,
> each to perform a particular set of functions. I'd like to have a few
> variables that are common to all forms, figuring that is the way to
> share the data among forms.
> 1. How do I make that work?
>
> 2. Is there a better approach to having some core data available from
> each form?
>
> 3. Is there a better way to organize the whole schmear?

I generally create a single public CGlobals class, instantiate it from Sub Main, and
expose properties from that.  This has the advantage of encapsulating data retrieval
and storage (from/to registry, INIs, whatever) for global data.
--
Working Without a .NET?
http://classicvb.org/petition
Author
21 Oct 2005 10:28 PM
Ken Halter
"Sharp" <some***@somewhere.net> wrote in message
news:lrpil1tdh1es7rugmftoqaa51i9lcodikp@4ax.com...
> I'm working on an application that I've set up with several forms,
> each to perform a particular set of functions. I'd like to have a few
> variables that are common to all forms, figuring that is the way to
> share the data among forms.
> 1. How do I make that work?

There are quite a few ways... the most straightforward would be to add a
..bas module to your app (Project/Add Module) and declare the variables
Public in there. Anything declared Public in a .bas module is available to
the entire app.

> 2. Is there a better approach to having some core data available from
> each form?

If you're careful, you can get away with the globals. They can really get
out of hand if you're not so careful. Since they're available to the whole
app, they're easily "stomped" on.

> 3. Is there a better way to organize the whole schmear?

It depends on a lot of things. You can create a class that contains all of
your "globals" and pass that around to who ever needs it, designate a
specific form to be that "class", add public properties/variables to that
form and treat it like a class, all kinds of different ways.

The bottom line is.... declare variables with as little scope as possible.
That way, finding code that manipulates those variables is much easier....
which means bugs are easier to find.

fwiw, here's a tool that'll tell you if you have something declared with
"Excessive Scope" (and about 1000 other details about your code) and show
you what to do to fix that situation. It'll say something like "Move the
variable named XXX to module YYY and change the declaration to Private".
Very cool tool... I have one of the older versions.

Project Analyzer v7.1
http://www.aivosto.com/project/project.html

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
21 Oct 2005 11:29 PM
MikeD
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message

> The bottom line is.... declare variables with as little scope as possible.
> That way, finding code that manipulates those variables is much easier....
> which means bugs are easier to find.

And usually significantly reduces the number of bugs caused by "stomping" on
a global variable.

Yes, global variables have their purpose and can be beneficial IF you use
them properly.  In my experience of 14+ years using VB, I've learned that
there is rarely a "genuine" need for a global variable.  There are almost
always alternatives that will make your code much better (tighter,
encapsulated, etc.).  You might have to write more code to avoid the use of
the global variable (and in a worst-case scenario, even radically change the
design of your program), but usually, you're better off.

--
Mike
Microsoft MVP Visual Basic