By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Declaring A Variable

P: n/a
In the Declarations section of a form's code module, what is the difference
between the following:
Dim Flag As Boolean

Public Flag As Boolean

Private Flag As Boolean

Thanks!

Lucy
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Lucy" <lg*****@earthlink.net> wrote in message
news:oN*****************@newsread2.news.atl.earthl ink.net...
In the Declarations section of a form's code module, what is the difference between the following: Dim Flag As Boolean
Private Flag As Boolean Either of these two declarations creates a static instance of the Boolean
variable "Flag" which is visible only to the form and not to the rest of the
application.
Public Flag As Boolean

This declaration creates a static instance of the Boolean variable "Flag"
which is visible to the entire application. For example, if you have a form
named "Form1" with the declaration statement "Public Flag As Boolean", the
"Flag" variable can be enumerated thus:
?Forms("Form1").Flag

NOTE: The form must be open for this variable to be available.

Hope this helps.
Nov 13 '05 #2

P: n/a
Thanks for the quick response!

Ok, I see the difference between Public and the other two. What is the
difference between Dim Flag and Private Flag? Is there a time when to use
one and not the other?

Lucy
"ByteMyzer" <sb*@nospam.news.chi.sbcglobal.net> wrote in message
news:Jv*****************@newssvr33.news.prodigy.co m...
"Lucy" <lg*****@earthlink.net> wrote in message
news:oN*****************@newsread2.news.atl.earthl ink.net...
In the Declarations section of a form's code module, what is the difference
between the following:

Dim Flag As Boolean
Private Flag As Boolean

Either of these two declarations creates a static instance of the Boolean
variable "Flag" which is visible only to the form and not to the rest of

the application.
Public Flag As Boolean This declaration creates a static instance of the Boolean variable "Flag"
which is visible to the entire application. For example, if you have a

form named "Form1" with the declaration statement "Public Flag As Boolean", the
"Flag" variable can be enumerated thus:
?Forms("Form1").Flag

NOTE: The form must be open for this variable to be available.

Hope this helps.

Nov 13 '05 #3

P: n/a
"Lucy" <lg*****@earthlink.net> wrote in message
news:rY*****************@newsread2.news.atl.earthl ink.net...
Thanks for the quick response!

Ok, I see the difference between Public and the other two. What is the
difference between Dim Flag and Private Flag? Is there a time when to use
one and not the other?

Lucy


As far as the declarations section of a code module are concerned, there is
significantly no difference between the Dim statement and the Private
statement. In the declarations section they serve the same purpose exactly
the same way.

However, the Private statement may only be used in the declarations section
of a module, whereas the Dim statement may be used to dynamically allocate a
variable within a procedure, in which case, the variable is only visible to
that procedure, and to no other part of the Module.
Nov 13 '05 #4

P: n/a
Lucy wrote:
Thanks for the quick response!

Ok, I see the difference between Public and the other two. What is the
difference between Dim Flag and Private Flag? Is there a time when to use
one and not the other?


(for short answer, skip to last para)

Back in the "good old days" Dim was used to "dimension" an array, it
still is but back then it was the only thing it was used for on BASIC,
other intrinsic variables were not declared at all.

With the advent of QuickBASIC (in the MS stable at least) we started
seeing the of "Dim" to declare variables, since "Dim" is short for
"Dimension" and intrinsic variables don't have dimensions (arrays do)
this was a pretty silly thing to use, but use it they did.

A lack of planning and/or slap happy ad-hoc development lead us down the
road of having global or shared variables in BASIC by using "Dim Shared"
in the module level to share a variable with all subs and functions,
"Shared" as a declaration in a sub/function to share it with the module
level code (entry point was at module level, no sub main back then) and
"common" to share across multiple modules although that declaration had
to exist in all modules that would see that variable. To have it in all
modules the use of an include file was quite slick.

The use of "Shared" seemed to disappear about the time of VB, probably
because of the way VB now used Sub Main or a form event as the entry
point to the program rather than the old fasioned way of module level
code and the use of "Dim" at the module level replaced "Dim Shared" and
"Global" replaced and enhanced "Common" as only one declaration was
required. (b4, "Dim" at the module level would make the variable
available to module level code only, not the subs and functions)

Later versions of VB started using "Private" and "Public" to distinguish
between module wide and global variables, this makes the module wide
declaration more explicit than "Dim", which apart from it's original use
to dimension an array, is there for backward compatability for intrinsic
variables.

--

\\\\\\
\\ \\ Windows is searching
\ \ For your sig.
\ \ Please Wait.
\__\

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.