472,350 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,350 software developers and data experts.

Issue with Private Functions

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?


Jan 31 '07 #1
7 4891
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"
Jan 31 '07 #2
"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...)

Feb 1 '07 #3

"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
%>
Feb 1 '07 #4

"Anthony Jones" <An*@yadayadayada.comwrote in message
news:ue**************@TK2MSFTNGP05.phx.gbl...
>
"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.

<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
%>
Feb 1 '07 #5
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"
Feb 1 '07 #6
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" <vi*@vic.comwrote in message
news:uD**************@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?


Feb 12 '07 #7
"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!!!

Feb 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jessica | last post by:
Hi, I have a question regarding the object-oriented issue. I understand that a class is a type. I have an array class. Now say that I want to...
12
by: Ken | last post by:
I am familiar with C and Java, I would like to use a style that I have become accustomed to in Java with C++ but each time I do this I have name...
10
by: E.T. Grey | last post by:
Hi, I have a C++ DLL that I want to use from a C# project. I am actually usng a lot of advanced C++ features like templates, partial/specialized...
7
by: Homa | last post by:
Hi, I'm thinking what will happen if two users access a page at the same time. If there are any local variable in the page, will this cause...
7
by: Dave | last post by:
Hi all, I have... using System.Collections; namespace MyApp.Templates { public class MyPage : System.Web.UI.Page { ArrayList MyArray = new...
8
by: Tapeesh | last post by:
I have a following piece of code. The code was compiled using g++ class A { public : virtual void fn() = 0; }; class B: virtual private A {
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of...
17
by: Jason Doucette | last post by:
I am converting a C-style unit into a C++ class. I have an implementation function that was defined in the .cpp file (so it was hidden from the...
17
by: Peng Yu | last post by:
Hi, I'm wondering if there is something in namespace like the 'private' keyword in class? I want to define some class or function that can...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.