Results 1 to 12 of 12

Thread: Public vs. Private

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    Is this true

    - When I want to use a variable through out the project - I should declare it Public in a module not a form.
    - When I want to use a variable through out the module - I should declare it Private in a module on a module level.

    Thanks
    Mako Shark
    Great White

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    This is correct. You can also use "Dim" at the module level to achieve the same effect, although "Private" is the preferred keyword.
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    Guest
    BruceG, why is "Private" preferred?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    In a form, you can use Public, Private and Dim on a module level - all will do the samething. Like Bruce, I prefer Private. Using Public in a form on a module level - does not make the variable public to all procedures throughout the project. It is only public to that form. It is only public throughout the entire project if it is declare in a module.

    Is that true Bruce? I tested out and that is what I got.

    Thanks for your reply Bruce and Caspian.



    Global Level - I use Public
    Module Level - I use Private
    Procedural Level - I use Dim

    [Edited by Shark on 10-10-2000 at 02:17 PM]
    Mako Shark
    Great White

  5. #5
    Guest
    That is what I use as well.

    Also, another tip is to try to avoid Public variables when possible.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    Thanks for the confirmation Megatron.

    I usually avoid Public too.
    Mako Shark
    Great White

  7. #7
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Shark - you said:
    In a form, you can use Public, Private and Dim on a module level - all will do the samething. Like Bruce, I prefer Private. Using Public in a form on a module level - does not make the variable public to all procedures throughout the project. It is only public to that form. It is only public throughout the entire project if it is declare in a module.
    Let me advise you that some of this is not quite right. The following applies to both forms and standard bas modules:
    On the module level, "Private" and "Dim" declare variables that can only be used in that module (all Subs or Functions in that module); whereas "Public" variables (which you guys correctly pointed out should be used sparingly if at all) can be referenced by any sub or function throughout the project.

    The thing with "Public" is that a variable declared as Public within a form must be "qualified" with the form name when used outside that form (i.e. "Form1.intMyVar").

    Caspian -
    I believe that "Private" is preferred over "Dim" at the module level so that you have a specific keyword to declare a variable at a specific scope - i.e., you can only use "Dim" at the procedure level to declare local variables; and we can choose to let "Private" be the keyword to declare module-level variables.



    "It's cold gin time again ..."

    Check out my website here.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    So Bruce, can I sum it up like this?


    Global Level
    Use Public
    -To reference a variable in a form that is declare Public, the form name must be referenced.
    -How come not modules but only forms?

    Module Level
    Use Private

    Procedural Level
    Use Dim
    Mako Shark
    Great White

  9. #9
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You got it. As far as the "why", I don't know. We'll have to ask Bill.
    "It's cold gin time again ..."

    Check out my website here.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247

    Thumbs up Thanks again Bruce

    Thanks for your time.
    Mako Shark
    Great White

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Forms are considered to be objects. If you declare a public variable in an object it becomes a property of that object.
    To set a property value you always have to use the refer to the object with its name and then the property name divided by a dot.
    You can use the name of a module as well to reference a public variable but you usually don't have to.
    Sometimes you do however. This is because VB always use the variable with the nearest scoop.
    Let's say you declare a public variable called iMyInt As Integer in a module called Module1 and then you declare a private variable in Form1 also called iMyInt.
    Then when you refer to iMyInt in Form1 the local variable is assumed. To use the public variable you have to type something like this:
    Code:
    Module1.iMyInt = 5
    Best regards

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    You brought up a good point Joacim. A form is an object and that is why it has to be referenced. Thanks for the explanatin of the nearest scope.

    Thanks.
    Mako Shark
    Great White

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