|
-
Jun 13th, 2000, 02:56 AM
#1
Thread Starter
Frenzied Member
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
-
Jun 13th, 2000, 03:00 AM
#2
So Unbanned
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.
-
Jun 13th, 2000, 03:09 AM
#3
Lively Member
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.
-------------------------
-
Jun 13th, 2000, 03:11 AM
#4
Thread Starter
Frenzied Member
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
-
Jun 13th, 2000, 03:13 AM
#5
Yes, i think it has to deal with the backward compatibility. The Public keyword is a little more flexible than Global.
-
Jun 13th, 2000, 04:58 AM
#6
transcendental analytic
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.
-
Jun 13th, 2000, 05:07 AM
#7
Lively Member
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.
-
Jun 13th, 2000, 05:15 AM
#8
Thread Starter
Frenzied Member
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
-
Jun 13th, 2000, 02:29 PM
#9
Frenzied Member
I agrre with you on that Seaweed.
Illuminator's use of public variables when they should just be Dim is just asking for trouble!
-
Jun 13th, 2000, 03:32 PM
#10
Frenzied Member
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."
-
Jun 13th, 2000, 05:54 PM
#11
Hyperactive Member
-
Jun 13th, 2000, 10:08 PM
#12
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.
-
Jun 13th, 2000, 10:47 PM
#13
Hyperactive Member
-
Jun 13th, 2000, 11:41 PM
#14
Member
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
-
Jun 14th, 2000, 04:00 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|