Issue with Private Functions

Issue with Private Functions

am 01.02.2007 00:14:14 von 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?

Re: Issue with Private Functions

am 01.02.2007 00:42:40 von reb01501

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


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Issue with Private Functions

am 01.02.2007 04:48:58 von Victor

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

Re: Issue with Private Functions

am 01.02.2007 10:05:47 von Anthony Jones

"Victor" wrote in message
news:ebAmsPbRHHA.2172@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 & " />"
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 &
"
"
End Function

Private CannotCallThisFromOutside()
Response.Write "CannotCallThisFromOutside Called
"
End Function

Public CallPrivateFunc()
Response.Write "CallPrivateFunc
"
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
%>

Re: Issue with Private Functions

am 01.02.2007 10:30:39 von Anthony Jones

"Anthony Jones" wrote in message
news:ueX8qAeRHHA.3948@TK2MSFTNGP05.phx.gbl...
>
> "Victor" wrote in message
> news:ebAmsPbRHHA.2172@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.
>
>



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 &
"
"
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 &
"
"
End Function

Private Function CannotCallThisFromOutside()
Response.Write "CannotCallThisFromOutside Called
"
End Function

Public Function CallPrivateFunc()
Response.Write "CallPrivateFunc
"
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
%>

Re: Issue with Private Functions

am 01.02.2007 13:03:34 von reb01501

Victor wrote:
>>> 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???
>

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.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Issue with Private Functions

am 12.02.2007 01:28:19 von 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



"Victor" wrote in message
news:uDDpL2YRHHA.4476@TK2MSFTNGP05.phx.gbl...
>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?
>
>
>
>
>
>

Re: Issue with Private Functions

am 12.02.2007 02:11:00 von Victor

"Anthony Jones" wrote...
>
> "Victor" wrote...
:
> > What, then, is the difference between a Private Function and a non-private
> > function?
>
> 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!!!