Results 1 to 15 of 15

Thread: Global vs. Public

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Question

    I am working on a project which has some variables declared as Public and others as Global (in a basic module). I have never used Global before.

    Can anyone tell me the difference between Global and Public declarations, and when you would use one over the other?

    Thanks in advance,

    ~seaweed

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Here's a simple overview:
    Public, can be used by all subs in the form, module, etc.
    Private, Only in that form, module, etc.
    Global, can be used by any form, module, etc. but can only be declared in modules.

  3. #3
    Lively Member
    Join Date
    Jan 1999
    Posts
    81

    Personal thoughts

    I don't like public var myself... They are resource hogs.. But there are the simplest to use...

    If you have a fairly large project i suggest using the global declare inside of a module...

    Just my $0.02
    -------------------------
    There is never only one right answer. That is the magic of programming.
    -------------------------

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Thanks for the quick reply, DiGiTalErRoR, but I guess I should clarify a little about what I already know...

    Similarities:
    Public's scope is project-wide.
    Global's scope is project-wide.

    Differences:
    Public can be used in a class or basic module.
    Global can only be used in a basic module.

    Questions:
    Is that the only difference (that you can't use Global in a class module)?
    Is there ever any reason why you would use Global instead of Public?
    Or is it just for backward compatibility with older version of Visual Basic (before there was a Public keyword)?

    Thanks,

    ~seaweed

  5. #5
    Guest
    Yes, i think it has to deal with the backward compatibility. The Public keyword is a little more flexible than Global.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Personally I never use Global, it's one keyword too much as Public does the same thing.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Lively Member
    Join Date
    May 2000
    Posts
    84
    I find having both public and global available quite useful actaully.

    When ever I know a variable will be used everywhere within
    the program I tend to use Global. If it is to be used just
    within the form or module I use public.

    This way when you look back at your code later you can get an idea of where you can expect to find the variables by the way they were declared.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    That doesn't make sense to me....

    Illuminator:

    If a variable is to be used only within a form or module, it should be declared Private. If you declare it Public, it can be used from anywhere within your project.

    Be careful if you are declaring variables that are only supposed to have Module or Form-level scope as Public, because any other form or module can access them!

    ~seaweed

  9. #9
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    I agrre with you on that Seaweed.

    Illuminator's use of public variables when they should just be Dim is just asking for trouble!



    Mark
    -------------------

  10. #10
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    I'll quote to you from "Pure Visual Basic" by Dan Fox - (a truly fantastic book for only £11.99 !)

    "Although the Dim statement can be used at the module level, it is equivalent to using Private and is included for backward compatibility with older versions of VB. The same can be said for Global, which is equivalent to Public."
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  11. #11
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Exclamation With respect....

    to Dan Fox, Global is NOT equivalent to Public. Not entirely anyway.

    For instance:

    Code:
    ***Form code***
    Option Explicit
    
    Public pString As String
    
    Private Sub Form_Load()
        pString = "Set in the form"
        gString = "Also set in the form"
        Call modset
    End Sub
    While in a module:

    Code:
    ***Module Code***
    Option Explicit
    
    Global gString As String
    
    Public Sub modset()
        Form1.pString = "Set in the module"
        gString = "Also set in the module"
    End Sub
    To use Public variables from outside their own (object) code module you need an extra indirection (i.e. Form1.pstring). Globals can be used anywhere without reference to where they are declared.

    I was given to understand that processing the indirection would in some cases make Public variables slower to use than global variables.




    Dan

    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.

  12. #12
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Global is exactly the same as Public. Global scope is absolete. You can also declare your variable as Public in the module and it will work the same way as Global.

  13. #13
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Exclamation That doesn't make much sense to me.

    I always use Global for variables I intend to be Global. As was mentioned above, it keeps them distinct from Public variables that aren't really public (i.e. those that need an indirection).

    (What is the point in a scope declaration if its behaviour is inconsistent?)

    I don't think its obsolete, its useful and make code more unnderstandable, and the concept of scope easier to grasp.

    Just my opinion of course.

    Dan

    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.

  14. #14

    Clarification

    What Serge is saying by "obsolete" is that the Global keyword is an old keyword still available to use for backwards compatibility. Now that VB offers more OOP processing, especially with Forms classes, you should be using the Public keyword instead. (It truly doesn't make a difference), except where those comments are true, indicating that Global can't be used in Forms (because of my previous statement).

    I have read all the messages, but I think everyone is trying to go a lot further with this than needs to be.

    Global = Public (and Public can be declared in more places and is a better keyword than Global) (Period)

    From the VB Help under Public we read...

    "Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside."

    Also look at this "Constant and Variable Naming Conventions" using the Search feature of the VB Help. Use the See Also link on this page to investigate more.

    These will answer a lot of these questions.

    "Key to growth is study, testing and finally, applying correct fundamental principles."



    [Edited by Jaguar on 06-14-2000 at 02:44 PM]
    Senior Systems Architect/Programmer

  15. #15
    Guest

    More clarification

    Jaguar is right when he says that this problem arises from VB's attempt to implement object-oriented programming .

    For those of you who program in C++:
    When you make a form in VB, you are creating a new class (ie. frmMyNewForm) which "inherits" from the Form class. frmMyNewForm does everything that a standard form does, but it adds it's own functions (private, protected, and public) and its own variables (also private, protected, and public).

    It's accurate enough for this discussion to think of each form as an instance of it's own class. By declaring a Public variable in a form, *you are making it a member of that class* and giving other classes (other forms) "permission" to use it. (Judd- this is why you need the extra indirection).

    The reason you can't declare a Global in a form is because a global class member doesn't make any sense (What if there is no instance of the class, ie. the form is Unloaded?) The reason you can put a Public variable in a module is simply to minimize the mistakes that a VB programmer can make. The compiler will handle it just like a Global.

    Anyway, moral of the story is that they'll work the same in VB, they just have different origins.

    [Edited by RoyceWindsor1 on 06-14-2000 at 05:03 PM]

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