Issue with Private Functions

V

Victor

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?
 
B

Bob Barrows [MVP]

Victor said:
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 ...
 
V

Victor

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...)
 
A

Anthony Jones

Victor said:
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
%>
 
A

Anthony Jones

Anthony Jones said:
so function,
then it non-private

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.

<rubbish code snipped>

Prehaps it better I test code before posting :p

<%
Dim a, b, c
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 Sub

Public Function ShowVariables()
Dim c
c = "Local"
Response.Write "ShowVariables; a:" & a & "; b:" & b & "; c:" & c &
"<br />"
End Function

Private Function CannotCallThisFromOutside()
Response.Write "CannotCallThisFromOutside Called <br />"
End Function

Public Function 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
%>
 
B

Bob Barrows [MVP]

Victor said:
I thought that, if a variable is defined outside of the Private
function, then it doesn't exist inside the function???

No, the Private keyword defines where the _function_ is available. The
variable was delared at the Page (module) level so it is available to all
code within the Page's scope. it is not available to any code running
outside the Page's scope. In ASP, this situation is unlikely unless you are
using classes.

It is the opposite that is true: if you declare a variable within a function
or sub, the variable is is only visible within the scope of the function or
sub. This code will cause the "undefined" error:

<%
option explicit
Function test()
Dim x1
x1=test
End Function
test
response.write x1
%>

Incidently, these keywords (Public|Private|etc.) can be used when declaring
variables, but as Anthony explains, in vbscript, especially when used in ASP
pages, there really is little point to using them.
What, then, is the difference between a Private Function and a
non-private function? (um, only one joke per reply, please...)

In vbscript, there really is no difference unless you are using classes, as
Anthony explains.
 
F

Farshad Hemmati

Victor,

The private function is within the scope of the variable X1, so it will be
included. Only outside of the scope (in classes) would you find X1 not
producing the same results.

However, if you define X1 again in the function, it will create a new
instance of it, and disregard the X1 of the higher level within that
function.

Farshad
 
V

Victor

... :

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.

Ah, I understand now, thanks!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,123
Messages
2,570,741
Members
47,296
Latest member
EarnestSme

Latest Threads

Top