473,811 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5090
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.comwro te in message
news:eb******** ******@TK2MSFTN GP04.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 "ModuleLevelFun c; a:" & a & "; b:" & b & "; c:" & c & "<br
/>"
End Function

Class MyClass
Private b
Sub Class_Initializ e()
b = "Module"
End Class

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

Private CannotCallThisF romOutside()
Response.Write "CannotCallThis FromOutside Called <br />"
End Function

Public CallPrivateFunc ()
Response.Write "CallPrivateFun c <br/>"
CannotCallThisF romOutside()
End Function
End Class

ModuleLevelFunc ()
Dim o
Set o = New MyClass
o.ShowVariables
o.CallPrivateFu nc
'o.b = "New Value" ' Uncomment this will result in error since b is private
in the class
'o.CannotCallTh isFromOutside() ' Another error since function is private in
the class
%>
Feb 1 '07 #4

"Anthony Jones" <An*@yadayadaya da.comwrote in message
news:ue******** ******@TK2MSFTN GP05.phx.gbl...
>
"Victor" <vi*@vic.comwro te in message
news:eb******** ******@TK2MSFTN GP04.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 "ModuleLevelFun c; a:" & a & "; b:" & b & "; c:" & c &
"<br/>"
End Function

Class MyClass
Private b
Sub Class_Initializ e()
b = "Module"
End Sub

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

Private Function CannotCallThisF romOutside()
Response.Write "CannotCallThis FromOutside Called <br />"
End Function

Public Function CallPrivateFunc ()
Response.Write "CallPrivateFun c <br/>"
CannotCallThisF romOutside()
End Function
End Class

ModuleLevelFunc ()
Dim o
Set o = New MyClass
o.ShowVariables
o.CallPrivateFu nc
'o.b = "New Value" ' Uncomment this will result in error since b is private
in the class
'o.CannotCallTh isFromOutside() ' 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.comwro te in message
news:uD******** ******@TK2MSFTN GP05.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
2139
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. Does it make sense to make a class for the algorithm such as class MyAlg { private: Array A1;
12
1584
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 by using different names can anyone tell me how to get the following to work: I want to have a class named v3d (vector 3d), it has the following private instance variables... float x, y, z; It has the following public get and set methods:
10
1445
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 using STL containers like std::string, std::vector and std::map quite extensively in my C++ DLL API. However, I can simplify the API so as to make it easier to use from C#. Below, is a very simple "proof of concept" C++ DLL. I would be extremely...
7
1135
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 call some functions, say logic.doSomthing(), and the class logic is a singleton (for simplicity), will there be problems?
7
1799
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
2442
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
1991
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, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have memory error. Thus the derived classes has additional functionality, not additional data.
17
2482
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 structure that is only needed by the implementation, so it were declared in the .cpp file, as well. Now, when converting this into a class, the class definition exists in the .h file, since it's required by the interface. The implementation...
17
30691
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
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.