Re: Using collection to store variable values

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Dale Fye wrote:
I was just playing around with this concept, I don't really need the key
values but was wondering whether there was a method to do so.

A while back, I read an article that advised use of custom functions to
store variables rather than declaring Global Variables. It explaained that:

1. some global variables will lose their values when some errors occur,
causing unintended problems
2. unlike functions, you cannot use global variables as parameters in
queries, unless you build the SQL dynamically and concatenate the value of
the variable.

It described how to create the function that allows you to both set and
retrieve a value stored in a static variable. I've found this extremely
useful but in one of my programs have about a dozen of these functions. I
was toying with the idea of extending that functionality to a static
collection defined within a function, so that instead of having to have
numerous functions, I'd just have this one function that gets passed a key,
and a value (to set the value) or a key (to retrieve the value). So far,
that functionality is working fine in the test program I've developed. Have
not had a chance to test reading and writing to it multiple times (for a
speed test comparison).

The problem with using an array is that you either have to keep track of
which position the variable is stored in, or have to search the array for
the value. At least with a collection, you can get the value as long as you
know the key. Hopefully, the code that supports the collection is optimized
for this search.


I agree with everthing above with the possible exception of
searching a collection being faster than searcing an array
(run some timing tests to verify or refute your assumption).

--
Marsh
MVP [MS Access]
.



Relevant Pages