Log in

View Full Version : [SOLVED:] Different ways to store module variables



DanFish
04-18-2010, 02:10 AM
Hi All,

Silly question, but what is the difference in storing variables at the module level, and simply calling them from any procedure, versus passing them in the procedure declaration i.e. sub procedure(byval variable as integer) etc.

Are there benefits to system memory, or a best practice to follow?

Thanks in advance.

Dan

Paul_Hossler
04-23-2010, 06:17 PM
Usually I find it's better to pass parms that will be used within a function or sub as as a procedure variable (first group below)

Makes it more moduler and much easier to debug




Dim X1 As Long, Y1 As Long
' passed as parameters

Sub UpperLevel()
Call LowerLevel(1, 2)
Call LowerLevel(3, 4)
Call LowerLevel(5, 6)
End Sub

Sub LowerLevel(X As Long, Y As Long)
MsgBox X * Y
End Sub

' passed as global
Sub UpperLevel_1()
X1 = 1
Y1 = 2
Call LowerLevel_1
X1 = 3 * X1
Y1 = 4 * Y1
Call LowerLevel_1
X1 = 5 * X1
Y1 = 6 * Y1
Call LowerLevel_1
End Sub

Sub LowerLevel_1()
MsgBox X1 * Y1
End Sub

' passed as global, but with error to track down
Sub UpperLevel_2()
X1 = 1
Y1 = 2
Call LowerLevel_1
' some more code that affects global variable X1
X1 = 1000
' some more code
X1 = 3 * X1
Y1 = 4 * Y1
Call LowerLevel_1
X1 = 5 * X1
Y1 = 6 * Y1
Call LowerLevel_1
End Sub


Paul

Bob Phillips
04-24-2010, 02:53 AM
I agree, I like to keep global and module variables to an absolute minimum.