Re: Where to declare variables
From: Roger Carlson (NO-Rog3erc-SPAM_at_hotmail.com)
Date: 08/20/04
- Next message: Vic: "Re: passing variables"
- Previous message: Douglas J. Steele: "Re: Prevent changing *.mdw file"
- In reply to: Harlan: "Where to declare variables"
- Messages sorted by: [ date ] [ thread ]
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 > >
- Next message: Vic: "Re: passing variables"
- Previous message: Douglas J. Steele: "Re: Prevent changing *.mdw file"
- In reply to: Harlan: "Where to declare variables"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|