473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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 5010
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 implement an algorithm A that uses the array class....
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 conflict. In the past I have just worked around it...
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 templates, functors and callbacks. I am also...
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 concurrency problem? Simarily, if this page need to...
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 ArrayList();
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 code, and may be reducing amount of code in binary,...
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 interface that exists in the .h file). It uses a...
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 only be used within that namespace. Thanks, Peng
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.