Re: Where to declare variables

From: Roger Carlson (NO-Rog3erc-SPAM_at_hotmail.com)
Date: 08/20/04


Date: Fri, 20 Aug 2004 08:54:46 -0400

Well, Scope IS the answer to your question. Where and how will the variable
be used? There is very little reason to declare a variable globally when it
is only used in one subroutine. Also, you can reuse variables that are
declared at the subroutine level.

For instance, I use "i" a lot as a counter variable. Suppose I have a
routine with a "FOR i = 1 to <somevalue>" NEXT Loop. Further, suppose that
in that loop, I have a call to another subroutine that also has a counter.
If "i" is declared globally, I can't use that in my other routine. So I
would have to create a different variable, even though that counter is only
used in one routine.

A similar problem develops with object variables: database objects or
recordset objects. If I use "rs" as a recordset variable, I could not use
that again anywhere in the database. Using it again, even to reference the
same dynaset, could have serious consequences. So I would have to have rs1,
rs2, ... etc. even to view essentially the same data.

In addition, good structured programming practice says that your variables
should have the smallest scope possible. This leads to the least amount of
confusion. I disagree that it would be easier to keep track of in a global
module. It is much easier to keep track of when it is as close as possible
to the code that uses it.

Lastly, in Access, Global Variables have a nasty habit of vanishing,
especially when errors occur. To get around this, I most often store global
variables (when I use them, which is not often) in a control on a hidden
form or in a temp table. They have more permanence there.

-- 
--Roger Carlson
  www.rogersaccesslibrary.com
  Reply to: Roger dot Carlson at Spectrum-Health dot Org
"Harlan" <Nevermind@ddd.com> wrote in message
news:eCpkcErhEHA.3944@tk2msftngp13.phx.gbl...
> I understand the difference in Dim, Private, Public (Global) as to the
SCOPE
> of the variable and where it can be used.
>
> My question is: What are the pros and cons of keeping a variable private
as
> apposed to declaring all variables as Public in one module. I am working
on
> an application which will end up with a large number of variables and it
> seems it would be "easier" to keep track of them if I declared them all in
> one place instead of at the form or procedure level.
>
> Thanks
>
>


Relevant Pages

  • Re: use module to pass data between procedures
    ... If a single scope has both an SZ as a dummy argument ... rename the module variable for the scope in question. ... means that you can't declare SZ twice as a module variable in the same ...
    (comp.lang.fortran)
  • Re: Garbage collection problem
    ... The whole point of optimization is to ... > same camp as Chris Smith on the matter. ... declare a scope for the variable I may be depending on the object remaining ...
    (comp.lang.java.programmer)
  • Please advise of basic problem in my code (using opendialog)
    ... Thankyou for your time Olie, I do understand the basics of classes, but ... One of those principals is variable scope. ... In your example you declare file in the function Form1_Loadbut this ... private void Form1_Load ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java needs "goto" (was Re: hi)
    ... cause they won't go out of scope. ... as if they are hit, you are COMPLETELY out to lunch, ... that exception was hit if you are using declarations ... so a "declare at the top" style should still yield to ...
    (comp.lang.java.programmer)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)