Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Global Conflict
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ron Kunce
04-27-05 08:55 PM


Re: Global Conflict
"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..



Report this thread to moderator Post Follow-up to this message
Old Post
Ken Halter
04-27-05 08:55 PM


Re: Global Conflict
"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..."


Report this thread to moderator Post Follow-up to this message
Old Post
Bob Butler
04-27-05 08:55 PM


Re: Global Conflict
"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....



Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Johnson [MVP: VB]
04-27-05 08:55 PM


Re: Global Conflict
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ron Kunce
04-27-05 08:55 PM


Re: Global Conflict
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Dick Grier
04-28-05 08:56 AM


Re: Global Conflict
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 ba
s
>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





Report this thread to moderator Post Follow-up to this message
Old Post
J French
04-28-05 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Visual Basic archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.