Home > Archive > Visual Basic > April 2005 > Global Conflict
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ron Kunce 2005-04-27, 3:55 pm |
| I inherited a VB application that sets a list of variables on a form as
Public (global across the application). It then sets an identical set of
variable declarations as Public on a second form. These variables are then
manipulated on a couple of other forms and modules. The designer uses a
form declaration prefix to specify which form's global variable is being
modified when setting its value. Each declaring form subsequently uses the
value, set elsewhere, within its own functions. Despite being a poor
design, it appears to work. However, I am curious as to how VB is managing
two application global variables with the same name. I would have thought
that VB would create only one memory location for a globally declared
variable and disallow or use the same memory for a second global declaration
of the same name.
| |
| Ken Halter 2005-04-27, 3:55 pm |
| "Ron Kunce" <AID9F5C@NoSpam.idpa.state.il.us> wrote in message
news:%23SjC%23C0SFHA.3184@TK2MSFTNGP14.phx.gbl...
>I inherited a VB application that sets a list of variables on a form as
> Public (global across the application). It then sets an identical set of
Variables declared Public in a form become properties of that form. They are
not global to the entire app. It's exactly the same as declaring a public
property in a class (which makes sense because forms are "specialized"
classes)
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
| |
| Bob Butler 2005-04-27, 3:55 pm |
| "Ron Kunce" <AID9F5C@NoSpam.idpa.state.il.us> wrote in message
news:%23SjC%23C0SFHA.3184@TK2MSFTNGP14.phx.gbl
> I inherited a VB application that sets a list of variables on a form
> as Public (global across the application). It then sets an identical
> set of variable declarations as Public on a second form. These
> variables are then manipulated on a couple of other forms and
> modules. The designer uses a form declaration prefix to specify
> which form's global variable is being modified when setting its
> value. Each declaring form subsequently uses the value, set
> elsewhere, within its own functions. Despite being a poor design, it
> appears to work. However, I am curious as to how VB is managing two
> application global variables with the same name. I would have
> thought that VB would create only one memory location for a globally
> declared variable and disallow or use the same memory for a second
> global declaration of the same name.
'Public' means 'Global' only when used in a BAS code module. When it is
used in a class or form it creates a public variable connected to each
instance of that object. Each copy of the variable is unique. For example,
if in Form1 you have:
Public x As Long
then you can do:
dim f1 as form1, f2 as form1
set f1=new form1
set f2=new form1
f1.x = 42
f2.x = 99
each instance of the form has a publicly exposed variable named 'x' and they
are unique.
To make it more complicated, it is possible in VB to have two BAS code
modules and declare the same global variable name in each one (using either
'Public' or 'Global'). If you do that you will get 'ambiguous name
detected' from code in other components unless you specify which one you
want as in Module1.x = Module2.x
Note that this probably isn't a good practice! <g>
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Jeff Johnson [MVP: VB] 2005-04-27, 3:55 pm |
|
"Ron Kunce" <AID9F5C@NoSpam.idpa.state.il.us> wrote in message
news:%23SjC%23C0SFHA.3184@TK2MSFTNGP14.phx.gbl...
>I inherited a VB application
And here I thought this was going to be a political thread given the subject
line....
| |
| Ron Kunce 2005-04-27, 3:55 pm |
| Thanks all! I am so used to declaring application global variables in a bas
module called Globals, that I didn't realize class module Public
declarations are different.
| |
| Dick Grier 2005-04-28, 3:56 am |
| Hi,
Public <> Global
Public variables are globally visible, but are properties (fields) of the
form class. Public variables in a Module are truly global.
Dick
--
Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
| |
| J French 2005-04-28, 3:56 am |
| On Wed, 27 Apr 2005 13:14:08 -0500, "Ron Kunce"
<AID9F5C@NoSpam.idpa.state.il.us> wrote:
>Thanks all! I am so used to declaring application global variables in a bas
>module called Globals, that I didn't realize class module Public
>declarations are different.
Not entirely relevant, but you might like this
In a BAS Module
Private Type TGLB ' <-- note the Private
Var1 As String
Var2 As Long
...
End Type
Public glb As TGLB ' <-- note the Public
Your global variables are now:
glb.Var1
glb.Var2
|
|
|
|
|