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
Printable View
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
This is correct. You can also use "Dim" at the module level to achieve the same effect, although "Private" is the preferred keyword.
BruceG, why is "Private" preferred?
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]
That is what I use as well.
Also, another tip is to try to avoid Public variables when possible.
Thanks for the confirmation Megatron.
I usually avoid Public too.
Shark - you said:
Let me advise you that some of this is not quite right. The following applies to both forms and standard bas modules:Quote:
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.
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.
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
You got it. As far as the "why", I don't know. We'll have to ask Bill. :)
Thanks for your time.
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:
Best regardsCode:Module1.iMyInt = 5
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.