Variable Declaration

paulmcdonnell

Ready to Help
Local time
Today, 04:23
Joined
Apr 11, 2001
Messages
167
**- - - - To late or to Early Bind - - - -**

Hi guys,

Here's one query that I've just never got round to asking but I do some much Automation now that I doing a lot of repetition.

If I'm running a lot of automation code which uses a great many variables (Hundred or so).

Which is better practice to open and delare all my variables throughout the program on startup and jsut keep re allocating them as they're needed
Or
To declare and release at the time of use when the event or function requires.

Is it a trade of between speed and memory useage or what.

I do alot of this now and curently I'm declaring and releasing all the way but it would seem easier if all i was doing was resetting already available variables

I hope that this makes sense

Cheers
Paul
;)
 
paulmcdonnell said:
Which is better practice to open and delare all my variables throughout the program on startup and jsut keep re allocating them as they're needed
Or
To declare and release at the time of use when the event or function requires.

Is it a trade of between speed and memory useage or what.

I would say that you declare variables when and where you need them. If you need something for the purpose of a sub or function then declare it within that sub/function.

If you need it with a few subs but only on one form then declare it at form level.

If you need it database wide then declare it Public in a module.

Creating loads of variables at startup just sounds silly and is a drain on resources.

When closing subs/functions try and reset them where possible - when I finish a function I set all strings to vbNullString and all objects to Nothing.
 
Best practice: Limit to minimum scope required

Paul,

Best programming practice is to declare variables for the mimimum scope required. For readability put all the dim's at the top of the procedure or function and ensure you use option explicit in every module so variables must be defined before use.
Mile-O-Philes advice shows specific examples of best practice. :)

Tony
 

Users who are viewing this thread

Back
Top Bottom