|
-
Apr 8th, 2006, 12:19 PM
#1
Thread Starter
Fanatic Member
Variable scope question.
OK, I'm cleaning up somebody else's code where the variable declarations are kind of a mess. None of the modules is Option Explicit, and there are a couple of places where the same variable is declared in multiple modules. So, my question is this: If a variable (let's call it vVar) is declared as private in the declarations of one module, and Public in another module, does the private declaration take precedence within the module it's declared in? Might be easier with an example:
VB Code:
'In Module1
Public vVar As Variant
Public Sub Example()
vVar = 1
Call OtherModule
End Sub
VB Code:
'In Module2
Private vVar As Variant
Public Sub OtherModule()
'Is vVar 1 or empty here?
End Sub
Hope that made sense.
-
Apr 8th, 2006, 12:29 PM
#2
Thread Starter
Fanatic Member
Re: Variable scope question.
Another related question. If nothing is Option Explicit, does anyone know how variables defined as public is Unloaded forms are treated? That is, if there is a form in the project with a public variable and it hasn't been loaded yet, does it get loaded if that variable is used by another module like when a property is called? If not, what happens to the existing variable if it is loaded?
VB Code:
'In Form1
Public vVar As Variant
VB Code:
'In Module1
'vVar is not declared.
Public Sub OtherModule()
'Does this load the form?
vVar = 1
'If not, what is the value and scope of vVar after this:
Form1.Show
End Sub
-
Apr 8th, 2006, 01:05 PM
#3
Re: Variable scope question.
 Originally Posted by Comintern
...does the private declaration take precedence within the module it's declared in? ....
Only vVar in Module 1 is changed.
-
Apr 8th, 2006, 01:07 PM
#4
Re: Variable scope question.
Option Explicit which you should always have in all modules has nothing to do with how the variables are treated; it only insures that all variables are defined.
-
Apr 8th, 2006, 01:17 PM
#5
Re: Variable scope question.
Yep, there is no way of using Module1's vVar in Module2 - as you cannot specify the scope (you could if it was based in a form or class).
In Post #2 I presume you meant to use "Form1.vVar = 1" rather than just "vVar = 1".
The way you have it will create a new (local) variable called vVar and set the value (if Option Explicit was set you would get an error instead), whereas the "Form1.vVar = 1" version will load the form if it isn't already loaded.
-
Apr 8th, 2006, 01:38 PM
#6
Thread Starter
Fanatic Member
Re: Variable scope question.
 Originally Posted by si_the_geek
Yep, there is no way of using Module1's vVar in Module2 - as you cannot specify the scope (you could if it was based in a form or class).
In Post #2 I presume you meant to use "Form1.vVar = 1" rather than just "vVar = 1".
The way you have it will create a new (local) variable called vVar and set the value (if Option Explicit was set you would get an error instead), whereas the "Form1.vVar = 1" version will load the form if it isn't already loaded.
OK, so I think I have this straight now. Given the same variable name, the one with the most local scope will be used. In the case of a public form variable, the form will only be loaded if it is explicitly referenced with the form object. That just leaves one last question. How would the second case work if the form is already loaded? I.e.:
VB Code:
'In Form1
Public vVar As Variant
Private Sub Form1_Initialize()
vVar = 1
Me.Hide
End Sub
VB Code:
'In Module1
'vVar not declared.
Private Sub Example()
Form1.Show
'Form1 hides itself, but is still loaded.
Debug.Print vVar '1 or 0?
Unload Form1
End Sub
-
Apr 8th, 2006, 01:44 PM
#7
Re: Variable scope question.
As you have not specified that you want to use the Forms variable (using "Form1.vVar"), you are not using it - in order to use a forms variable outside of the form you have to specify the form as well as the variable.
As with the previous case, a new local variable will be created.
I would strongly recommend adding Option Explicit to the modules etc, as it will highlight any issues like this.
-
Apr 8th, 2006, 01:59 PM
#8
Thread Starter
Fanatic Member
Re: Variable scope question.
I actually always use Option Explicit, avoid variable naming conflicts, etc., so I'm always a little baffled at what is going on when I'm dealing with someone's code who doesn't.
I'm currently working on debugging somebody else's project where all of the variable declarations are a mess and thought that explicitly declaring all the variables would be a good first step. Was just having a hard time figuring out what scope I should give everything.
Thanks, this helps a lot.
-
Apr 8th, 2006, 10:54 PM
#9
Re: Variable scope question.
I wish you the best of luck. I'm still in the process of adding Option Explicit to someone else's code and watching it blow up. After 5-1/2 months.
I can't say that going to work isn't interesting.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|