Results 1 to 6 of 6
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Public Variables (VB 6)

    I must be missing something. I have a module (Module 1.bas) defined within which are several functions. I also have entered some public variables in the module like the following:

    Public sngFast As Single

    In one of my forms I have the following code:

    sngFast = 300

    But when I run the program the form's code stops and declares that the variable sngFast is not defined.

    Am I using the correct syntax in defining the public variable? What am I missing here?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public Variables (VB 6)

    That looks correct. Are you sure that Module1.bas is part of the same project that contains the form?

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Public Variables (VB 6)

    It's working now, but I'm not quite sure what I did other than close, restart the application and move the declaration of my public variables to the top of the Module instead of below some other public subs. I don't see what their position in the module has to do with anything, but I'm happy I can continue my development.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public Variables (VB 6)

    Declarations of variables and constants (if not within a procedure of function) should always be at the top of a module, before all procedures (subs) and functions.

  5. #5
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Public Variables (VB 6)

    Thanks once again Hans,

    My little VB6 for Dummies book didn't make that requirement clear.

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Variables (VB 6)

    I don't think it's obvious, but it appears that VB wants all such declarations in a contiguous block, at the beginning of a module, after any Option statements and before any procedures. This is consistent with the way in which the compiler/ interpreter parses the module in terms of Objects and Procedures e.g.

    (General)
    (Declarations)
    Sub1
    Sub2
    Userform
    Click
    Initialize
    etc.

    A simple test shows that any declarations outside of the (Declarations) block are not "accounted for" as declarations. Not sure how they're dealt with in running code, but the implicit or explicit variable scope would probably not end up as intended.

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •