Public Variables in Module

D

David

Hello

I have been using a Public Variable in a Standard Module to save the hassle
of having to pass a variable (generated in one form but used in another)
between the two forms since the old form closes as the new one opens.

I use the variable (called WhereString) several times while I have the form
open to generate several updates based on the same information.

I have been reading poasts lately that refer to Public Variables in Standard
Modules as a programming faux pax.
Is it possible that there is a legitimate use for them?
If not is there a proper way to do what I am doing?

Always ready to learn better ways
David
 
A

Allen Browne

This is a place for public variables, e.g. when passing something into an
existing event procedure where you don't have a choice, such as Report_Open.

In most cases, though, you are better to explicitly pass the local variable
to another procedure, e.g.:
Function Multiply(x As Double, y As Double) As Double
Multiply = x * y
End function
and then call it as:
a = 6
b = 4
c = Multiply(a,b)

The major weaknesses of the public variable are:
a) They can be unintentially modified by any procedure in the project -
especially serious where there are multiple developers, which can introduce
some hard-to-debug problems.

b) They are reset when you Run | Reset during debugging.

My personal approach is to use them only when absolutely necessary.
 
D

David

Thanks Allen

I think I will continue using this one then. It works well and I'm the only
one programming. I should probably add a remark in saying where the variable
is declared to cover myself for later.

Up until recently I have been relying on Standard Modules too much but since
I have been reading up about this I think Class Modules have been very handy.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top