I have several places in my project where I declare a variable in a
procedure and give it a value, only to do basically the same thing in
another
procedure in the same module. In some cases, I've named it something else
(in the second procedure). I think in some I've actually used the same
name.
Actually, the real goal in software development is to isolate everything as
much
as possible. So, local variables, and NOT sharing them between pieces of
code
is actually a very good. The reason for this is that if you change some code
in one place, you don't accidentally cause something else to break.
Of course, if you need to share some data between routines, then the above
goal changes. However, you often seem someone ask a question how to
pass a value between forms, or return a value from a form, and the
suggestion is to use a global variable. however, that is bad choice IF YOU
can easily change your code, and NOT use a global var. If you avoid
globally shared variables, then you don't have to look in a zillion places
when trying to find a bug. Further, you can cut, or copy code between
different routines...and again since it more isolated, the resulting code is
more robust. And, further, you can copy code, or forms between
different applications, and again you don't have to remember to copy some
variable that was declared in a module. So, as a good rule, and concept,
you do want to avoid global variables.
I thought if I took the variable declaration up to the module (module
behind
a form, in probably all the important cases) declarations area up top,
declared it and gave it a value, that I could then use it in any procedure
in
the module.
Yes, the above is correct. Note that the variable will ONLY able to be used
by that particular form...other forms will not see that value. However, what
you
say above is correct, and I not sure why it not working.
I tried it and it didn't work. Why not? I'd really like to go through
and
clean up my variables so I don't needlessly declare/value them numerous
times
or have more than one variable with the same value.
Well, lets assume we have a form, and in the code module we have:
Option Compare Database
Option Explicit
-- note that you REALLY REALLY REALLY want to ensure that you ALWAYS use
Option Explicit. that option forces you to declare all variables..and if you
don't use it..then a simply miss-typing of a variable will NOT be found
until you run the code. with option explicit, then every time you make
changes to you code...you go debug->compile.
Ok, so, lets assume:
Option Compare Database
Option Explicit
dim i as interger
Sub Test1
i = 1
do while i < 10
i = i + 1
Call Test2
loop
end sub
Sub Test2
for i = 1 to 5
......
end sub
If you look at the above,we have instant bugs because the 2nd routine also
uses the variable i, and will mess up our first piece of code. So, that
problem you complain of having to declare variables over and over is not
such
a bad thing. The better approach is to use what we call local variables:
Option Compare Database
Option Explicit
Sub Test1
dim i as integer
i = 1
do while i < 10
i = i + 1
Call Test2
loop
end sub
Sub Test2
dim i as interger
for i = 1 to 5
......
end sub
In the above, you can see that each routine uses the "i" variable..but they
are self contained, local to the one routine, and also isolated from each
other. that means I can safely modify the code, but not have to worry about
a zillion other routines perhaps, or possible using the same variable.
Further,
I can freely cut and past that code into other parts of the applications,
and
even copy the code to other applications without fear of breaks. A very
large portion of developer time is wasted tracking down bugs...so, the more
you isolate your code, and avoid those global, then less chance of something
else breaking it..
So, the way things work are:
The so called "scope" form the top most all the way down is:
The variable if declared in the eh sub, or function is used. When the sub or
function exits..then he variables are discard. next in line is the forms
code modules variables..and when the form close..then all of those variables
are
destroyed. And, then the final bottom most is the standard code modules.....
Note that in above, all of the routines (local subs, functions) and then the
forms module and then standard code modules CAN ALL have a variable declared
with the SAME NAME. So, the variable at the top most declare is the one
that has the scope.
If you don't declare the variables in the routine..then it
uses the next lower possible declaring for that variable. So, if you don't
declare it local to the routine..then the model level is used. And, if this
is form model..and the variable is not declare..then we go down further to
the
standard modules.
Note that if you declare a variable in a standard module like:
dim i as interger
Then ANY piece of code in that module can use the above variable. We
consider the variable LOCAL to that one module. If you want EVERY MODULE,
and
EVERY form module to be able to see and use that variable..simply declare it
as public
public i as integer