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
Re: Variable scope question.
Quote:
Originally Posted by Comintern
...does the private declaration take precedence within the module it's declared in? ....
Only vVar in Module 1 is changed.
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.
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.
Re: Variable scope question.
Quote:
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
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.
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.
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.