|
-
Oct 10th, 2000, 12:43 PM
#1
Thread Starter
Addicted Member
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
-
Oct 10th, 2000, 12:47 PM
#2
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.
-
Oct 10th, 2000, 01:02 PM
#3
BruceG, why is "Private" preferred?
-
Oct 10th, 2000, 01:09 PM
#4
Thread Starter
Addicted Member
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]
-
Oct 10th, 2000, 03:18 PM
#5
That is what I use as well.
Also, another tip is to try to avoid Public variables when possible.
-
Oct 10th, 2000, 05:51 PM
#6
Thread Starter
Addicted Member
Thanks for the confirmation Megatron.
I usually avoid Public too.
-
Oct 10th, 2000, 06:46 PM
#7
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.
-
Oct 10th, 2000, 07:20 PM
#8
Thread Starter
Addicted Member
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
-
Oct 10th, 2000, 10:15 PM
#9
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.
-
Oct 10th, 2000, 10:40 PM
#10
Thread Starter
Addicted Member
Thanks again Bruce
-
Oct 11th, 2000, 03:39 AM
#11
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 regards
-
Oct 11th, 2000, 01:18 PM
#12
Thread Starter
Addicted Member
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.
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
|