|
 |
access thread: Global vs Public Variables
Message #1 by "enzo" <enzaux@g...> on Mon, 22 Apr 2002 03:58:11 +0800
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_0058_01C1E9B1.EB9B6F00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I know this quite a dumb question. But I'm confused when to use a
Global or a Public variable :) any ideas when I should use a global or a
public variables? I tired to search in MSDN but it seems I can't find
aparticular topic that would explain it all.
Thanks,
Enzo
Message #2 by "dave sharpe" <davesharpe2@c...> on Sun, 21 Apr 2002 16:04:41 -0400
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_0009_01C1E94E.3F2B2E80
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Enzo -
I hope that this helps, it is from Access97 Help.
Public was the last area discussed. As that is your
primary concern, I've moved it to the beginning
of the discussion.
Dave
=3D=3D=3D=3D=3D=3D
Understanding Scope and Visibility
=3D=3D=3D=3D=3D=3D
Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available to all
procedures in the project. In the following example, the string variable
strMsg can be used by any procedure in any module in the project.
' Include in Declarations section of module.
Public strMsg As String
All procedures are public by default, except for event procedures. When
Visual Basic creates an event procedure, the Private keyword is
automatically inserted before the procedure declaration. For all other
procedures, you must explicitly declare the procedure with the Private
keyword if you do not want it to be public.
You can use public procedures, variables, and constants defined in
standard modules or class modules from referencing projects. However,
you must first set a reference to the project in which they are defined.
Public procedures, variables, and constants defined in other than
standard or class modules, such as form modules or report modules, are
not available to referencing projects, because these modules are private
to the project in which they reside.
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
Scope refers to the availability of a variable, constant, or procedure
for use by another procedure. There are three scoping levels:
procedure-level, private module-level, and public module-level.
You determine the scope of a variable when you declare it. It's a good
idea to declare all variables explicitly to avoid naming-conflict errors
between variables with different scopes.
Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible outside
that procedure. Only the procedure that contains the variable
declaration can use it. In the following example, the first procedure
displays a message box that contains a string. The second procedure
displays a blank message box because the variable strMsg is local to the
first procedure.
Sub LocalVariable()
Dim strMsg As String
strMsg =3D "This variable can't be used outside this procedure."
MsgBox strMsg
End Sub
Sub OutsideScope()
MsgBox strMsg
End Sub
Defining Private Module-Level Scope
You can define module-level variables and constants in the Declarations
section of a module. Module-level variables can be either public or
private. Public variables are available to all procedures in all modules
in a project; private variables are available only to procedures in that
module. By default, variables declared with the Dim statement in the
Declarations section are scoped as private. However, by preceding the
variable with the Private keyword, the scope is obvious in your code.
In the following example, the string variable strMsg is available to any
procedures defined in the module. When the second procedure is called,
it displays the contents of the string variable strMsg in a dialog box.
' Add following to Declarations section of module.
Private strMsg sAs String
Sub InitializePrivateVariable()
strMsg =3D "This variable can't be used outside this module."
End Sub
Sub UsePrivateVariable()
MsgBox strMsg
End Sub
Note Public procedures in a standard module or class module are
available to any referencing project. To limit the scope of all
procedures in a module to the current project, add an Option Private
Module statement to the Declarations section of the module. Public
variables and procedures will still be available to other procedures in
the current project, but not to referencing projects.
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 3:58 PM
Subject: [access] Global vs Public Variables
I know this quite a dumb question. But I'm confused when to use a
Global or a Public variable :) any ideas when I should use a global or a
public variables? I tired to search in MSDN but it seems I can't find
aparticular topic that would explain it all.
Thanks,
Enzo
Message #3 by "enzo" <enzaux@g...> on Mon, 22 Apr 2002 05:25:18 +0800
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_00C8_01C1E9BE.1776BC40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Thanks, Dave but still it doesn't differentiate or state the pro's and
con's of using a global or a public variable
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 4:04 AM
Subject: [access] Re: Global vs Public Variables
Enzo -
I hope that this helps, it is from Access97 Help.
Public was the last area discussed. As that is your
primary concern, I've moved it to the beginning
of the discussion.
Dave
=3D=3D=3D=3D=3D=3D
Understanding Scope and Visibility
=3D=3D=3D=3D=3D=3D
Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available to
all procedures in the project. In the following example, the string
variable strMsg can be used by any procedure in any module in the
project.
' Include in Declarations section of module.
Public strMsg As String
All procedures are public by default, except for event procedures.
When Visual Basic creates an event procedure, the Private keyword is
automatically inserted before the procedure declaration. For all other
procedures, you must explicitly declare the procedure with the Private
keyword if you do not want it to be public.
You can use public procedures, variables, and constants defined in
standard modules or class modules from referencing projects. However,
you must first set a reference to the project in which they are defined.
Public procedures, variables, and constants defined in other than
standard or class modules, such as form modules or report modules, are
not available to referencing projects, because these modules are private
to the project in which they reside.
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
Scope refers to the availability of a variable, constant, or procedure
for use by another procedure. There are three scoping levels:
procedure-level, private module-level, and public module-level.
You determine the scope of a variable when you declare it. It's a good
idea to declare all variables explicitly to avoid naming-conflict errors
between variables with different scopes.
Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible
outside that procedure. Only the procedure that contains the variable
declaration can use it. In the following example, the first procedure
displays a message box that contains a string. The second procedure
displays a blank message box because the variable strMsg is local to the
first procedure.
Sub LocalVariable()
Dim strMsg As String
strMsg =3D "This variable can't be used outside this procedure."
MsgBox strMsg
End Sub
Sub OutsideScope()
MsgBox strMsg
End Sub
Defining Private Module-Level Scope
You can define module-level variables and constants in the
Declarations section of a module. Module-level variables can be either
public or private. Public variables are available to all procedures in
all modules in a project; private variables are available only to
procedures in that module. By default, variables declared with the Dim
statement in the Declarations section are scoped as private. However, by
preceding the variable with the Private keyword, the scope is obvious in
your code.
In the following example, the string variable strMsg is available to
any procedures defined in the module. When the second procedure is
called, it displays the contents of the string variable strMsg in a
dialog box.
' Add following to Declarations section of module.
Private strMsg sAs String
Sub InitializePrivateVariable()
strMsg =3D "This variable can't be used outside this module."
End Sub
Sub UsePrivateVariable()
MsgBox strMsg
End Sub
Note Public procedures in a standard module or class module are
available to any referencing project. To limit the scope of all
procedures in a module to the current project, add an Option Private
Module statement to the Declarations section of the module. Public
variables and procedures will still be available to other procedures in
the current project, but not to referencing projects.
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 3:58 PM
Subject: [access] Global vs Public Variables
I know this quite a dumb question. But I'm confused when to use a
Global or a Public variable :) any ideas when I should use a global or a
public variables? I tired to search in MSDN but it seems I can't find
aparticular topic that would explain it all.
Thanks,
Enzo
Message #4 by "dave sharpe" <davesharpe2@c...> on Sun, 21 Apr 2002 17:40:19 -0400
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_0020_01C1E95B.9ABC1E00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Enzo - if you don't need to use a global, don't; as it
consumes more resources than one declared locally.
Dave
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 5:25 PM
Subject: [access] Re: Global vs Public Variables
Thanks, Dave but still it doesn't differentiate or state the pro's and
con's of using a global or a public variable
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 4:04 AM
Subject: [access] Re: Global vs Public Variables
Enzo -
I hope that this helps, it is from Access97 Help.
Public was the last area discussed. As that is your
primary concern, I've moved it to the beginning
of the discussion.
Dave
=3D=3D=3D=3D=3D=3D
Understanding Scope and Visibility
=3D=3D=3D=3D=3D=3D
Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available to
all procedures in the project. In the following example, the string
variable strMsg can be used by any procedure in any module in the
project.
' Include in Declarations section of module.
Public strMsg As String
All procedures are public by default, except for event procedures.
When Visual Basic creates an event procedure, the Private keyword is
automatically inserted before the procedure declaration. For all other
procedures, you must explicitly declare the procedure with the Private
keyword if you do not want it to be public.
You can use public procedures, variables, and constants defined in
standard modules or class modules from referencing projects. However,
you must first set a reference to the project in which they are defined.
Public procedures, variables, and constants defined in other than
standard or class modules, such as form modules or report modules, are
not available to referencing projects, because these modules are private
to the project in which they reside.
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
Scope refers to the availability of a variable, constant, or
procedure for use by another procedure. There are three scoping levels:
procedure-level, private module-level, and public module-level.
You determine the scope of a variable when you declare it. It's a
good idea to declare all variables explicitly to avoid naming-conflict
errors between variables with different scopes.
Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible
outside that procedure. Only the procedure that contains the variable
declaration can use it. In the following example, the first procedure
displays a message box that contains a string. The second procedure
displays a blank message box because the variable strMsg is local to the
first procedure.
Sub LocalVariable()
Dim strMsg As String
strMsg =3D "This variable can't be used outside this procedure."
MsgBox strMsg
End Sub
Sub OutsideScope()
MsgBox strMsg
End Sub
Defining Private Module-Level Scope
You can define module-level variables and constants in the
Declarations section of a module. Module-level variables can be either
public or private. Public variables are available to all procedures in
all modules in a project; private variables are available only to
procedures in that module. By default, variables declared with the Dim
statement in the Declarations section are scoped as private. However, by
preceding the variable with the Private keyword, the scope is obvious in
your code.
In the following example, the string variable strMsg is available to
any procedures defined in the module. When the second procedure is
called, it displays the contents of the string variable strMsg in a
dialog box.
' Add following to Declarations section of module.
Private strMsg sAs String
Sub InitializePrivateVariable()
strMsg =3D "This variable can't be used outside this module."
End Sub
Sub UsePrivateVariable()
MsgBox strMsg
End Sub
Note Public procedures in a standard module or class module are
available to any referencing project. To limit the scope of all
procedures in a module to the current project, add an Option Private
Module statement to the Declarations section of the module. Public
variables and procedures will still be available to other procedures in
the current project, but not to referencing projects.
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 3:58 PM
Subject: [access] Global vs Public Variables
I know this quite a dumb question. But I'm confused when to use a
Global or a Public variable :) any ideas when I should use a global or a
public variables? I tired to search in MSDN but it seems I can't find
aparticular topic that would explain it all.
Thanks,
Enzo
Message #5 by "enzo" <enzaux@g...> on Mon, 22 Apr 2002 06:02:25 +0800
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_00DC_01C1E9C3.466FA160
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
When it is timely to use global? I'm confused because those variable
declared public can be used on the entire program then what does a
variable decalred in global can be used of?
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 5:40 AM
Subject: [access] Re: Global vs Public Variables
Enzo - if you don't need to use a global, don't; as it
consumes more resources than one declared locally.
Dave
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 5:25 PM
Subject: [access] Re: Global vs Public Variables
Thanks, Dave but still it doesn't differentiate or state the pro's
and con's of using a global or a public variable
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 4:04 AM
Subject: [access] Re: Global vs Public Variables
Enzo -
I hope that this helps, it is from Access97 Help.
Public was the last area discussed. As that is your
primary concern, I've moved it to the beginning
of the discussion.
Dave
=3D=3D=3D=3D=3D=3D
Understanding Scope and Visibility
=3D=3D=3D=3D=3D=3D
Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available
to all procedures in the project. In the following example, the string
variable strMsg can be used by any procedure in any module in the
project.
' Include in Declarations section of module.
Public strMsg As String
All procedures are public by default, except for event procedures.
When Visual Basic creates an event procedure, the Private keyword is
automatically inserted before the procedure declaration. For all other
procedures, you must explicitly declare the procedure with the Private
keyword if you do not want it to be public.
You can use public procedures, variables, and constants defined in
standard modules or class modules from referencing projects. However,
you must first set a reference to the project in which they are defined.
Public procedures, variables, and constants defined in other than
standard or class modules, such as form modules or report modules, are
not available to referencing projects, because these modules are private
to the project in which they reside.
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
Scope refers to the availability of a variable, constant, or
procedure for use by another procedure. There are three scoping levels:
procedure-level, private module-level, and public module-level.
You determine the scope of a variable when you declare it. It's a
good idea to declare all variables explicitly to avoid naming-conflict
errors between variables with different scopes.
Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible
outside that procedure. Only the procedure that contains the variable
declaration can use it. In the following example, the first procedure
displays a message box that contains a string. The second procedure
displays a blank message box because the variable strMsg is local to the
first procedure.
Sub LocalVariable()
Dim strMsg As String
strMsg =3D "This variable can't be used outside this procedure."
MsgBox strMsg
End Sub
Sub OutsideScope()
MsgBox strMsg
End Sub
Defining Private Module-Level Scope
You can define module-level variables and constants in the
Declarations section of a module. Module-level variables can be either
public or private. Public variables are available to all procedures in
all modules in a project; private variables are available only to
procedures in that module. By default, variables declared with the Dim
statement in the Declarations section are scoped as private. However, by
preceding the variable with the Private keyword, the scope is obvious in
your code.
In the following example, the string variable strMsg is available
to any procedures defined in the module. When the second procedure is
called, it displays the contents of the string variable strMsg in a
dialog box.
' Add following to Declarations section of module.
Private strMsg sAs String
Sub InitializePrivateVariable()
strMsg =3D "This variable can't be used outside this module."
End Sub
Sub UsePrivateVariable()
MsgBox strMsg
End Sub
Note Public procedures in a standard module or class module are
available to any referencing project. To limit the scope of all
procedures in a module to the current project, add an Option Private
Module statement to the Declarations section of the module. Public
variables and procedures will still be available to other procedures in
the current project, but not to referencing projects.
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 3:58 PM
Subject: [access] Global vs Public Variables
I know this quite a dumb question. But I'm confused when to use
a Global or a Public variable :) any ideas when I should use a global or
a public variables? I tired to search in MSDN but it seems I can't find
aparticular topic that would explain it all.
Thanks,
Enzo
Message #6 by "dave sharpe" <davesharpe2@c...> on Sun, 21 Apr 2002 18:27:04 -0400
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_0024_01C1E962.22ACC7A0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Enzo I thought that you were using "global" as an alternative
term for "public". I know of no variable type named "global".
I did a search on the help data that I sent and "global" is
not in it. When I searched for "Global" access returned
the data for "Public"
I was able to create the following; "Global aaa As Integer"
( which surprised me ). I'll have to defer to someone more
knowledgeable than I, but at the moment I'll guess that "Global"
was actively used in some prior version of Assess and was kept
for compatibility.
Dave
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 6:02 PM
Subject: [access] Re: Global vs Public Variables
When it is timely to use global? I'm confused because those variable
declared public can be used on the entire program then what does a
variable decalred in global can be used of?
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 5:40 AM
Subject: [access] Re: Global vs Public Variables
Enzo - if you don't need to use a global, don't; as it
consumes more resources than one declared locally.
Dave
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 5:25 PM
Subject: [access] Re: Global vs Public Variables
Thanks, Dave but still it doesn't differentiate or state the pro's
and con's of using a global or a public variable
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 4:04 AM
Subject: [access] Re: Global vs Public Variables
Enzo -
I hope that this helps, it is from Access97 Help.
Public was the last area discussed. As that is your
primary concern, I've moved it to the beginning
of the discussion.
Dave
=3D=3D=3D=3D=3D=3D
Understanding Scope and Visibility
=3D=3D=3D=3D=3D=3D
Defining Public Module-Level Scope
If you declare a module-level variable as public, it's available
to all procedures in the project. In the following example, the string
variable strMsg can be used by any procedure in any module in the
project.
' Include in Declarations section of module.
Public strMsg As String
All procedures are public by default, except for event
procedures. When Visual Basic creates an event procedure, the Private
keyword is automatically inserted before the procedure declaration. For
all other procedures, you must explicitly declare the procedure with the
Private keyword if you do not want it to be public.
You can use public procedures, variables, and constants defined
in standard modules or class modules from referencing projects. However,
you must first set a reference to the project in which they are defined.
Public procedures, variables, and constants defined in other
than standard or class modules, such as form modules or report modules,
are not available to referencing projects, because these modules are
private to the project in which they reside.
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
Scope refers to the availability of a variable, constant, or
procedure for use by another procedure. There are three scoping levels:
procedure-level, private module-level, and public module-level.
You determine the scope of a variable when you declare it. It's
a good idea to declare all variables explicitly to avoid naming-conflict
errors between variables with different scopes.
Defining Procedure-Level Scope
A variable or constant defined within a procedure is not visible
outside that procedure. Only the procedure that contains the variable
declaration can use it. In the following example, the first procedure
displays a message box that contains a string. The second procedure
displays a blank message box because the variable strMsg is local to the
first procedure.
Sub LocalVariable()
Dim strMsg As String
strMsg =3D "This variable can't be used outside this
procedure."
MsgBox strMsg
End Sub
Sub OutsideScope()
MsgBox strMsg
End Sub
Defining Private Module-Level Scope
You can define module-level variables and constants in the
Declarations section of a module. Module-level variables can be either
public or private. Public variables are available to all procedures in
all modules in a project; private variables are available only to
procedures in that module. By default, variables declared with the Dim
statement in the Declarations section are scoped as private. However, by
preceding the variable with the Private keyword, the scope is obvious in
your code.
In the following example, the string variable strMsg is
available to any procedures defined in the module. When the second
procedure is called, it displays the contents of the string variable
strMsg in a dialog box.
' Add following to Declarations section of module.
Private strMsg sAs String
Sub InitializePrivateVariable()
strMsg =3D "This variable can't be used outside this module."
End Sub
Sub UsePrivateVariable()
MsgBox strMsg
End Sub
Note Public procedures in a standard module or class module
are available to any referencing project. To limit the scope of all
procedures in a module to the current project, add an Option Private
Module statement to the Declarations section of the module. Public
variables and procedures will still be available to other procedures in
the current project, but not to referencing projects.
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 3:58 PM
Subject: [access] Global vs Public Variables
I know this quite a dumb question. But I'm confused when to
use a Global or a Public variable :) any ideas when I should use a
global or a public variables? I tired to search in MSDN but it seems I
can't find aparticular topic that would explain it all.
Thanks,
Enzo
Message #7 by "enzo" <enzaux@g...> on Mon, 22 Apr 2002 07:09:45 +0800
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_011D_01C1E9CC.AF049D80
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
OIC Thanks Dave !!!!
Enzo :)
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 6:27 AM
Subject: [access] Re: Global vs Public Variables
Enzo I thought that you were using "global" as an alternative
term for "public". I know of no variable type named "global".
I did a search on the help data that I sent and "global" is
not in it. When I searched for "Global" access returned
the data for "Public"
I was able to create the following; "Global aaa As Integer"
( which surprised me ). I'll have to defer to someone more
knowledgeable than I, but at the moment I'll guess that "Global"
was actively used in some prior version of Assess and was kept
for compatibility.
Dave
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 6:02 PM
Subject: [access] Re: Global vs Public Variables
When it is timely to use global? I'm confused because those
variable declared public can be used on the entire program then what
does a variable decalred in global can be used of?
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 5:40 AM
Subject: [access] Re: Global vs Public Variables
Enzo - if you don't need to use a global, don't; as it
consumes more resources than one declared locally.
Dave
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 5:25 PM
Subject: [access] Re: Global vs Public Variables
Thanks, Dave but still it doesn't differentiate or state the
pro's and con's of using a global or a public variable
Thanks,
Enzo
----- Original Message -----
From: dave sharpe
To: Access
Sent: Monday, April 22, 2002 4:04 AM
Subject: [access] Re: Global vs Public Variables
Enzo -
I hope that this helps, it is from Access97 Help.
Public was the last area discussed. As that is your
primary concern, I've moved it to the beginning
of the discussion.
Dave
=3D=3D=3D=3D=3D=3D
Understanding Scope and Visibility
=3D=3D=3D=3D=3D=3D
Defining Public Module-Level Scope
If you declare a module-level variable as public, it's
available to all procedures in the project. In the following example,
the string variable strMsg can be used by any procedure in any module in
the project.
' Include in Declarations section of module.
Public strMsg As String
All procedures are public by default, except for event
procedures. When Visual Basic creates an event procedure, the Private
keyword is automatically inserted before the procedure declaration. For
all other procedures, you must explicitly declare the procedure with the
Private keyword if you do not want it to be public.
You can use public procedures, variables, and constants
defined in standard modules or class modules from referencing projects.
However, you must first set a reference to the project in which they are
defined.
Public procedures, variables, and constants defined in other
than standard or class modules, such as form modules or report modules,
are not available to referencing projects, because these modules are
private to the project in which they reside.
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D
Scope refers to the availability of a variable, constant, or
procedure for use by another procedure. There are three scoping levels:
procedure-level, private module-level, and public module-level.
You determine the scope of a variable when you declare it.
It's a good idea to declare all variables explicitly to avoid
naming-conflict errors between variables with different scopes.
Defining Procedure-Level Scope
A variable or constant defined within a procedure is not
visible outside that procedure. Only the procedure that contains the
variable declaration can use it. In the following example, the first
procedure displays a message box that contains a string. The second
procedure displays a blank message box because the variable strMsg is
local to the first procedure.
Sub LocalVariable()
Dim strMsg As String
strMsg =3D "This variable can't be used outside this
procedure."
MsgBox strMsg
End Sub
Sub OutsideScope()
MsgBox strMsg
End Sub
Defining Private Module-Level Scope
You can define module-level variables and constants in the
Declarations section of a module. Module-level variables can be either
public or private. Public variables are available to all procedures in
all modules in a project; private variables are available only to
procedures in that module. By default, variables declared with the Dim
statement in the Declarations section are scoped as private. However, by
preceding the variable with the Private keyword, the scope is obvious in
your code.
In the following example, the string variable strMsg is
available to any procedures defined in the module. When the second
procedure is called, it displays the contents of the string variable
strMsg in a dialog box.
' Add following to Declarations section of module.
Private strMsg sAs String
Sub InitializePrivateVariable()
strMsg =3D "This variable can't be used outside this module."
End Sub
Sub UsePrivateVariable()
MsgBox strMsg
End Sub
Note Public procedures in a standard module or class module
are available to any referencing project. To limit the scope of all
procedures in a module to the current project, add an Option Private
Module statement to the Declarations section of the module. Public
variables and procedures will still be available to other procedures in
the current project, but not to referencing projects.
----- Original Message -----
From: enzo
To: Access
Sent: Sunday, April 21, 2002 3:58 PM
Subject: [access] Global vs Public Variables
I know this quite a dumb question. But I'm confused when to
use a Global or a Public variable :) any ideas when I should use a
global or a public variables? I tired to search in MSDN but it seems I
can't find aparticular topic that would explain it all.
Thanks,
Enzo
---
Change your mail options at http://p2p.wrox.com/manager.asp
or
Message #8 by braxis@b... on Mon, 22 Apr 2002 00:26:16
|
|
Enzo
After a little testing I've come to the conclusion that there is
absolutely no difference between the Public and Global keywords.
The keyword Global isn't included in the Access XP help files, and I
suspect it's a leftover from an old, not quite so VB compliant, version of
Access. Maybe it's time I stopped using it then...
When should you use a Public variable? My old IT Lecturers answer would
be 'Never'!. Variables should be passed to functions and subroutines as
parameters and a single function should only calculate a single piece of
information which can then be passed back as the result. Information can
be passed between forms using the 'Property Let' and 'Property Get'
procedure declarations. Information that needs to persist throughout a
database session can be written to a table, using a pair of functions to
write and retrieve the information.
The main threat with using Public variables is their lack of transparancy.
Imagine trying to debug someones complex function; you suddenly come
across a variable that wasn't declared within the function. You search for
it and finally find that it was declared as a public variable in some
other module - but you also find 20 other places within the program where
that variable has it's value changed! It would be an absolute nightmare to
work out what it's value was supposed to be, and whether it was going to
be the same the next time the function ran...
So, save the Public keyword for constant declarations, where it can
actually enhance the readability and ease the debugging of your program.
Brian
> This is a multi-part message in MIME format.
------=_NextPart_000_00DC_01C1E9C3.466FA160
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
When it is timely to use global? I'm confused because those variable
declared public can be used on the entire program then what does a
variable decalred in global can be used of?
Thanks,
Enzo
Message #9 by "enzo" <enzaux@y...> on Mon, 22 Apr 2002 07:51:59 +0800
|
|
Brian,
Thanks you very much! If it's ok, can you give me a very simple sample
of using Property Let and Property Get to transfer info from one form to
another. Evernsince these two commands confused me even while I'm studying
doingClass modules.
Thanks for the info,
Enzo :)
PS Thank you also DAve
----- Original Message -----
From: <braxis@b...>
To: Access <access@p...>
Sent: Monday, April 22, 2002 12:26 AM
Subject: [access] Re: Global vs Public Variables
> Enzo
>
> After a little testing I've come to the conclusion that there is
> absolutely no difference between the Public and Global keywords.
>
> The keyword Global isn't included in the Access XP help files, and I
> suspect it's a leftover from an old, not quite so VB compliant, version of
> Access. Maybe it's time I stopped using it then...
>
> When should you use a Public variable? My old IT Lecturers answer would
> be 'Never'!. Variables should be passed to functions and subroutines as
> parameters and a single function should only calculate a single piece of
> information which can then be passed back as the result. Information can
> be passed between forms using the 'Property Let' and 'Property Get'
> procedure declarations. Information that needs to persist throughout a
> database session can be written to a table, using a pair of functions to
> write and retrieve the information.
>
> The main threat with using Public variables is their lack of transparancy.
> Imagine trying to debug someones complex function; you suddenly come
> across a variable that wasn't declared within the function. You search for
> it and finally find that it was declared as a public variable in some
> other module - but you also find 20 other places within the program where
> that variable has it's value changed! It would be an absolute nightmare to
> work out what it's value was supposed to be, and whether it was going to
> be the same the next time the function ran...
>
> So, save the Public keyword for constant declarations, where it can
> actually enhance the readability and ease the debugging of your program.
>
> Brian
>
> > This is a multi-part message in MIME format.
>
> ------=_NextPart_000_00DC_01C1E9C3.466FA160
> Content-Type: text/plain;
> charset="iso-8859-1"
> Content-Transfer-Encoding: quoted-printable
>
> When it is timely to use global? I'm confused because those variable
> declared public can be used on the entire program then what does a
> variable decalred in global can be used of?
>
> Thanks,
>
> Enzo
Message #10 by "enzo" <enzaux@g...> on Mon, 22 Apr 2002 07:53:07 +0800
|
|
Brian,
Thanks you very much! If it's ok, can you give me a very simple sample
of using Property Let and Property Get to transfer info from one form to
another. Evernsince these two commands confused me even while I'm studying
doingClass modules.
Thanks for the info,
Enzo :)
PS Thank you also DAve
> ----- Original Message -----
> From: <braxis@b...>
> To: Access <access@p...>
> Sent: Monday, April 22, 2002 12:26 AM
> Subject: [access] Re: Global vs Public Variables
>
>
> > Enzo
> >
> > After a little testing I've come to the conclusion that there is
> > absolutely no difference between the Public and Global keywords.
> >
> > The keyword Global isn't included in the Access XP help files, and I
> > suspect it's a leftover from an old, not quite so VB compliant, version
of
> > Access. Maybe it's time I stopped using it then...
> >
> > When should you use a Public variable? My old IT Lecturers answer would
> > be 'Never'!. Variables should be passed to functions and subroutines as
> > parameters and a single function should only calculate a single piece of
> > information which can then be passed back as the result. Information can
> > be passed between forms using the 'Property Let' and 'Property Get'
> > procedure declarations. Information that needs to persist throughout a
> > database session can be written to a table, using a pair of functions to
> > write and retrieve the information.
> >
> > The main threat with using Public variables is their lack of
transparancy.
> > Imagine trying to debug someones complex function; you suddenly come
> > across a variable that wasn't declared within the function. You search
for
> > it and finally find that it was declared as a public variable in some
> > other module - but you also find 20 other places within the program
where
> > that variable has it's value changed! It would be an absolute nightmare
to
> > work out what it's value was supposed to be, and whether it was going to
> > be the same the next time the function ran...
> >
> > So, save the Public keyword for constant declarations, where it can
> > actually enhance the readability and ease the debugging of your program.
> >
> > Brian
> >
> > > This is a multi-part message in MIME format.
> >
> > ------=_NextPart_000_00DC_01C1E9C3.466FA160
> > Content-Type: text/plain;
> > charset="iso-8859-1"
> > Content-Transfer-Encoding: quoted-printable
> >
> > When it is timely to use global? I'm confused because those variable
> > declared public can be used on the entire program then what does a
> > variable decalred in global can be used of?
> >
> > Thanks,
> >
> > Enzo
>
Message #11 by "Leo Scott" <leoscott@c...> on Sun, 21 Apr 2002 19:12:50 -0700
|
|
Public Property Let SomeVariableName(NewValue as SomeVariableType)
mvarSomeVariableName= NewValue
End Property
(If the variable type is an object you must use Set instead of Let)
Public Progerty Get SomeVariableName() as SomeVariableType
SomeVariableName=mvarSomeVariableName
End Property
The variable type passed to the Let/Set statement must be the same type used
as a return value for the Get statement. Property Let/Set/Get is like
having a public variable with much more control. I use the example below to
make zip files with a command line PKZIP25.exe. When the user of the class
sends me the path for the zip file I check it to make sure it has the
trailing \ and if it doesn't I add it.
Public Property Let ZIPPATH(ByVal NewZipPath As String)
mstrZipPath = NewZipPath
'make sure the path ends with a \
If Right(mstrZipPath, 1) <> "\" Then mstrZipPath = mstrZipPath & "\"
End Property
The advantage to classes and properties is you have much more control and
can do other things when the user sets a property.
|-----Original Message-----
|From: enzo [mailto:enzaux@g...]
|Sent: Sunday, April 21, 2002 4:53 PM
|To: Access
|Subject: [access] Re: Global vs Public Variables
|
|
| Brian,
|
| Thanks you very much! If it's ok, can you give me a very simple sample
| of using Property Let and Property Get to transfer info from one form to
| another. Evernsince these two commands confused me even while
|I'm studying
| doingClass modules.
|
| Thanks for the info,
|
| Enzo :)
|
| PS Thank you also DAve
|
|> ----- Original Message -----
|> From: <braxis@b...>
|> To: Access <access@p...>
|> Sent: Monday, April 22, 2002 12:26 AM
|> Subject: [access] Re: Global vs Public Variables
|>
|>
|> > Enzo
|> >
|> > After a little testing I've come to the conclusion that there is
|> > absolutely no difference between the Public and Global keywords.
|> >
|> > The keyword Global isn't included in the Access XP help files, and I
|> > suspect it's a leftover from an old, not quite so VB compliant, version
|of
|> > Access. Maybe it's time I stopped using it then...
|> >
|> > When should you use a Public variable? My old IT Lecturers answer would
|> > be 'Never'!. Variables should be passed to functions and subroutines as
|> > parameters and a single function should only calculate a
|single piece of
|> > information which can then be passed back as the result.
|Information can
|> > be passed between forms using the 'Property Let' and 'Property Get'
|> > procedure declarations. Information that needs to persist throughout a
|> > database session can be written to a table, using a pair of
|functions to
|> > write and retrieve the information.
|> >
|> > The main threat with using Public variables is their lack of
|transparancy.
|> > Imagine trying to debug someones complex function; you suddenly come
|> > across a variable that wasn't declared within the function. You search
|for
|> > it and finally find that it was declared as a public variable in some
|> > other module - but you also find 20 other places within the program
|where
|> > that variable has it's value changed! It would be an absolute nightmare
|to
|> > work out what it's value was supposed to be, and whether it
|was going to
|> > be the same the next time the function ran...
|> >
|> > So, save the Public keyword for constant declarations, where it can
|> > actually enhance the readability and ease the debugging of
|your program.
|> >
|> > Brian
|> >
|> > > This is a multi-part message in MIME format.
|> >
|> > ------=_NextPart_000_00DC_01C1E9C3.466FA160
|> > Content-Type: text/plain;
|> > charset="iso-8859-1"
|> > Content-Transfer-Encoding: quoted-printable
|> >
|> > When it is timely to use global? I'm confused because those variable
|> > declared public can be used on the entire program then what does a
|> > variable decalred in global can be used of?
|> >
|> > Thanks,
|> >
|> > Enzo
|>
|
|
|
|
|
Message #12 by "enzo" <enzaux@g...> on Mon, 22 Apr 2002 11:23:33 +0800
|
|
Thanks Leo I'll take a look on this
Enzo
----- Original Message -----
From: Leo Scott <leoscott@c...>
To: Access <access@p...>
Sent: Monday, April 22, 2002 10:12 AM
Subject: [access] Re: Global vs Public Variables
>
> Public Property Let SomeVariableName(NewValue as SomeVariableType)
> mvarSomeVariableName= NewValue
> End Property
> (If the variable type is an object you must use Set instead of Let)
>
>
> Public Progerty Get SomeVariableName() as SomeVariableType
> SomeVariableName=mvarSomeVariableName
> End Property
>
> The variable type passed to the Let/Set statement must be the same type
used
> as a return value for the Get statement. Property Let/Set/Get is like
> having a public variable with much more control. I use the example below
to
> make zip files with a command line PKZIP25.exe. When the user of the
class
> sends me the path for the zip file I check it to make sure it has the
> trailing \ and if it doesn't I add it.
>
> Public Property Let ZIPPATH(ByVal NewZipPath As String)
> mstrZipPath = NewZipPath
> 'make sure the path ends with a \
> If Right(mstrZipPath, 1) <> "\" Then mstrZipPath = mstrZipPath & "\"
> End Property
>
> The advantage to classes and properties is you have much more control and
> can do other things when the user sets a property.
>
> |-----Original Message-----
> |From: enzo [mailto:enzaux@g...]
> |Sent: Sunday, April 21, 2002 4:53 PM
> |To: Access
> |Subject: [access] Re: Global vs Public Variables
> |
> |
> | Brian,
> |
> | Thanks you very much! If it's ok, can you give me a very simple
sample
> | of using Property Let and Property Get to transfer info from one form to
> | another. Evernsince these two commands confused me even while
> |I'm studying
> | doingClass modules.
> |
> | Thanks for the info,
> |
> | Enzo :)
> |
> | PS Thank you also DAve
> |
> |> ----- Original Message -----
> |> From: <braxis@b...>
> |> To: Access <access@p...>
> |> Sent: Monday, April 22, 2002 12:26 AM
> |> Subject: [access] Re: Global vs Public Variables
> |>
> |>
> |> > Enzo
> |> >
> |> > After a little testing I've come to the conclusion that there is
> |> > absolutely no difference between the Public and Global keywords.
> |> >
> |> > The keyword Global isn't included in the Access XP help files, and I
> |> > suspect it's a leftover from an old, not quite so VB compliant,
version
> |of
> |> > Access. Maybe it's time I stopped using it then...
> |> >
> |> > When should you use a Public variable? My old IT Lecturers answer
would
> |> > be 'Never'!. Variables should be passed to functions and subroutines
as
> |> > parameters and a single function should only calculate a
> |single piece of
> |> > information which can then be passed back as the result.
> |Information can
> |> > be passed between forms using the 'Property Let' and 'Property Get'
> |> > procedure declarations. Information that needs to persist throughout
a
> |> > database session can be written to a table, using a pair of
> |functions to
> |> > write and retrieve the information.
> |> >
> |> > The main threat with using Public variables is their lack of
> |transparancy.
> |> > Imagine trying to debug someones complex function; you suddenly come
> |> > across a variable that wasn't declared within the function. You
search
> |for
> |> > it and finally find that it was declared as a public variable in some
> |> > other module - but you also find 20 other places within the program
> |where
> |> > that variable has it's value changed! It would be an absolute
nightmare
> |to
> |> > work out what it's value was supposed to be, and whether it
> |was going to
> |> > be the same the next time the function ran...
> |> >
> |> > So, save the Public keyword for constant declarations, where it can
> |> > actually enhance the readability and ease the debugging of
> |your program.
> |> >
> |> > Brian
> |> >
> |> > > This is a multi-part message in MIME format.
> |> >
> |> > ------=_NextPart_000_00DC_01C1E9C3.466FA160
> |> > Content-Type: text/plain;
> |> > charset="iso-8859-1"
> |> > Content-Transfer-Encoding: quoted-printable
> |> >
> |> > When it is timely to use global? I'm confused because those variable
> |> > declared public can be used on the entire program then what does a
> |> > variable decalred in global can be used of?
> |> >
> |> > Thanks,
> |> >
> |> > Enzo
> |>
> |
> |
> |
> |
> |
>
>
|
|
 |