"Victor" <vi*@vic.comwrote in message
news:eb**************@TK2MSFTNGP04.phx.gbl...
"Bob Barrows [MVP]" wrote...
Victor wrote:
I just tried a test comparing a Function to a Private Function with
this code:
>
<%
Option Explicit
dim X1
X1 = 9
>
Private Function RealTest(here)
RealTest = here + X1
End Function
>
Response.Write "The answer is " & RealTest(10)
%>
>
I get "The answer is 19". If the function is defined as private,
shouldn't I have gotten an error in the function that states that X1
is undefined?
Why should you? The function is contained in the page being executed so
it
is definitely within scope ...
I thought that, if a variable is defined outside of the Private function,
then it
doesn't exist inside the function???
What, then, is the difference between a Private Function and a non-private
function?
(um, only one joke per reply, please...)
Outside of a class the Private/Public qualifierto a Function it of no
consequence.
These prefixes define whether code external to module in which the function
is declared has the ability to call the function. Since an ASP script such
as you presented it just one big 'module' it doesn't matter whether the
functions are declared with private or public.
The prefixes do not impact variables with in the function. All variable
declared within a function are 'private' (the proper term is 'Local') to the
function. All variables declared outside of a function are available to all
code inside and outside of functions.
Public/Private only become useful inside Class definitions giving control
over which functions are actually callable by code outside the class and
which can only be used by code inside the class.
<%
Option Explicit
Dim a
a = "Global"
b = "Global"
c = "Global"
Function ModuleLevelFunc()
Dim b
b = "Local"
Response.Write "ModuleLevelFunc; a:" & a & "; b:" & b & "; c:" & c & "<br
/>"
End Function
Class MyClass
Private b
Sub Class_Initialize()
b = "Module"
End Class
Public ShowVariables()
Dim c
c = "Local"
Response.Write "ShowVariables; a:" & a & "; b:" & b & "; c:" & c &
"<br />"
End Function
Private CannotCallThisFromOutside()
Response.Write "CannotCallThisFromOutside Called <br />"
End Function
Public CallPrivateFunc()
Response.Write "CallPrivateFunc <br/>"
CannotCallThisFromOutside()
End Function
End Class
ModuleLevelFunc()
Dim o
Set o = New MyClass
o.ShowVariables
o.CallPrivateFunc
'o.b = "New Value" ' Uncomment this will result in error since b is private
in the class
'o.CannotCallThisFromOutside() ' Another error since function is private in
the class
%>