Results 1 to 9 of 9

Thread: Variable scope question.

  1. #1

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. 'In Module1
    2. Public vVar As Variant
    3.  
    4. Public Sub Example()
    5.  
    6.     vVar = 1
    7.     Call OtherModule
    8.  
    9. End Sub
    VB Code:
    1. 'In Module2
    2. Private vVar As Variant
    3.  
    4. Public Sub OtherModule()
    5.  
    6.     'Is vVar 1 or empty here?
    7.  
    8. End Sub
    Hope that made sense.

  2. #2

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. 'In Form1
    2. Public vVar As Variant

    VB Code:
    1. 'In Module1
    2. 'vVar is not declared.
    3.  
    4. Public Sub OtherModule()
    5.  
    6.     'Does this load the form?
    7.     vVar = 1
    8.    
    9.     'If not, what is the value and scope of vVar after this:
    10.     Form1.Show
    11.  
    12. End Sub

  3. #3

  4. #4

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  6. #6

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. 'In Form1
    2. Public vVar As Variant
    3.  
    4. Private Sub Form1_Initialize()
    5.  
    6.     vVar = 1
    7.     Me.Hide
    8.  
    9. End Sub
    VB Code:
    1. 'In Module1
    2. 'vVar not declared.
    3.  
    4. Private Sub Example()
    5.  
    6.     Form1.Show
    7.     'Form1 hides itself, but is still loaded.
    8.     Debug.Print vVar    '1 or 0?
    9.     Unload Form1
    10.  
    11. End Sub

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  8. #8

    Thread Starter
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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
  •  



Click Here to Expand Forum to Full Width