Ouch! Well, I never claimed to be good. Albert, could you help me out by
elaborating on why it is not a good idea to use global variables for this
sort of thing?
My global comments were not actaully direclted to you! It is common
to see solutiosn that use glaobls.
However, let me elinoate on globals:
In fact, if you can avoid global for passing values, you always
should.
First, if you use global vars, then you got some variables and defines that
are "out there" some where. In other words, we got two forms, and some code.
Now, we got also to look, worry about, and have some *knowledge* of some
other variables that we are using, and they are not part of our current
code solution. Think of your self as a developer going into the project
without prior knowledge as to how things work. When you got global vars all
over the place, it makes it VERY HARD to understand the code. So, instead
of two forms and some code, we added in a 3rd code module with some global
variable defs.
Global vars means your code is not modular. You spend some of your precious
time making two nice forms work together. A few months later, you go..hey, I
need those two forms to do the same thing in another project. You import the
two forms...and they don't work, because now you have to also go and hunt
down some global vars. It is question of ease of maintainability of your
application
that globals effect. The reverse is also true. You decide to import some
forms into your application. You as a habit may use the "same" global vars
to
pass values between forms. Now, you got more then one set of forms that use
those same global vars...and you don't know this...a real nice way to
introduce bugs...
Further, we all know that forms allow multiple instances. You might have a
design where you are looking at a customer, and the phone rings. Your users
can minimize the current customer form, and then bring up another customer
(ms-access allows multiple copies of the SAME form to be loaded at the same
time). So, now, if your design decides that you can have two copies, or
more of the same form opened at the same time, then once again globals will
not work..will they? Which set of forms opened do the globals vars apply to?
So, in a windows type environment, we allow multiple instances, and again
globals will kill any design that allows multiple instances of the same
window.
Even worse, we all OFTEN make a copy of the forms to do something similar in
the application. Another part of the application may be VERY similar to the
current part we are working on, and thus we copy the forms, and start
working away. The problem is now, we got this global dependency to worry
about in the code, and again if the other forms get used, then we got a
problem with the *same* global vars being used. So, globals are not a very
modular approach to coding. I mean, for example we can go:
globalParm1 = "Albert Kallal"
Call MyCoolSub
Public Sub MyCoolSub
msgbox "name passed = " & globalParm1
It is MUCH better to go:
Call MyCoolSub("Albert Kallal")
Public Sub MyCoolSub(strName as string)
msgbox "name passed = " & strName
I mean, ask your self why do we not normally use globals in the above
example? (we, CAN use globals...but we DO NOT!). As a rule, one
likely avoids using globals as above since the code is
INDEPENDENT of everything else. We can copy that code, we can modify that
code
without fear that we "forgot" or left out some global var. We also don't
have to
worry that if we copy, or modify the code, then we are breaking something
else.
The whole reason why parameters are an advantage between subs and functions
is
that we thus don't have to define global vars to pass information.
If you got code that uses globals all over the place, then it becomes VERY
hard to change ONE thing without effecting a zillion other things. Hum, I
am about to change one variable, is there 200 other subs and forms that
use the variables? Will changing the value effect every single sub, (or in
our
case form) in the whole application? Can I use it...can i change it?
The key to maintainable code and applications is to reduce the dependences
and the
zillion number of places to look for, and the zillion number of places that
can cause your code to break. You change a value of a global var that
something else is using, then you got a big problem.
So, simply passing values between two subroutines, or passing
values between two forms, the exact same philosophy, and good concepts of
development apply.
Now, without question, globals should, and can be used for things are
global. Things like user name, logon information etc. These things are
needed
by the whole application, and thus make sense to be globals.
However, to pass a simple value between two subs, or between two forms,
globals simply introduces dependencies and increases the difficulty at
which you can re-use code, and maintain the project.