PDA

View Full Version : Variables and scope



Jfp87
05-12-2015, 01:56 AM
Guys,

The number of variables I have in my project is beginning to mount and I am still not completely satisfied with how I am using them with regards to being local/public. I am running into some problems and I feel my variables are scattered everywhere.

This is my current thinking although I am starting to get a bit confused:

Any variables which are used between modules, I have defined as Public in the module which calls my userform.
Any variables which are used throughout a module have been defined in the general declarations area of that module using Dim (which in this case is identical to Private?).
Any variables which are needed in a procedure only have been defined within that procedure using Dim.

So if I am entering data into an array which has to be available to all modules and procedures for the duration of the running of the userform, I have declared them as "Public" in the module which calls my userform.

So if I am using counters i, j, x, y etc. in multiple modules, should I be declaring them in each procedure? or privately for the module to save re-dimensioning them each time?

My thought was that they should be re-dimensioned each time to ensure they are being destroyed when not needed.

Anyway, my job today will be to go through my code and try to organise all my variables correctly. Any tips / help on the best way to do this would be a great help.

Cheers guys.

Paul_Hossler
05-14-2015, 05:54 PM
If I have a lot of globally scoped variables, I tend to have a specific module just to hold them as well as any global Const's -- I just find it easy to housekeep them that way

Any counters that are used in a procedure, I Dim inside the procedure

Personal Style

SamT
05-14-2015, 08:50 PM
I do not recommend using global variables to pass information between one procedure and the next. That is what Procedure Parameters are for. It is too easy to introduce very hard to find bugs when using variables to pass data.

Global Constants are an entirely different subject. They will help prevent typos and make it very easy to maintain your code base when, for example, company information changes.


Const CompPublicPhNum As String = "(800) 555-1212"
Const CompPresidentName As String = "Superman"
Const CompAddress As String = "1 Broadway, Ste 1"
'Etc
In fact, all business rules should be kept as constants in one location.

My general rule is that it is far better to type "Dim Something As String" 100 times, than to have it left with a value from some other sub even once a year.

Paul_Hossler
05-15-2015, 07:54 AM
I do not recommend using global variables to pass information between one procedure and the next. That is what Procedure Parameters are for. It is too easy to introduce very hard to find bugs when using variables to pass data.
/ (http://www.askvg.com/review-whats-new-in-microsoft-office-2016/)

Agree that the use of Globals should be avoided as much as possible (esp if you can use a passed parameter), but sometimes you just gotta use 'em

SamT
05-15-2015, 09:07 AM
Yep, Sometimes I just can't figure out a way to avoid them. But, I try really hard.