473,881 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about public function

Hi,

In the code-behind, i have this function:

Public Function myfunction(ByVa l myvar As Object) As String
dim x as string = myvar
.....
Return x
End Function

My question is: can i use this function in other aspx / code-behind pages of
my application? Or, till where is this function accessible?
thanks
Gilbert
Feb 24 '07 #1
9 1784
The function is only available while that page (class) is loaded, which is
only while the page is being requested. You could place a function like
this (along with other functions you wish to call repeatedly) in their own
assembly (.dll) and then just reference that assembly from your ASP.NET
project, make an instance of the class that contains them and run them as
you need to.
"Gilbert" <gi@bert.sdwrot e in message
news:Os******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

In the code-behind, i have this function:

Public Function myfunction(ByVa l myvar As Object) As String
dim x as string = myvar
.....
Return x
End Function

My question is: can i use this function in other aspx / code-behind pages
of my application? Or, till where is this function accessible?
thanks
Gilbert

Feb 25 '07 #2
Thanks,

What is then the meaning of 'Public'?. If the function would be 'Private',
would that change its reach?
"Scott M." <s-***@nospam.nosp amschreef in bericht
news:e7******** *****@TK2MSFTNG P06.phx.gbl...
The function is only available while that page (class) is loaded, which is
only while the page is being requested. You could place a function like
this (along with other functions you wish to call repeatedly) in their own
assembly (.dll) and then just reference that assembly from your ASP.NET
project, make an instance of the class that contains them and run them as
you need to.
"Gilbert" <gi@bert.sdwrot e in message
news:Os******** ******@TK2MSFTN GP06.phx.gbl...
>Hi,

In the code-behind, i have this function:

Public Function myfunction(ByVa l myvar As Object) As String
dim x as string = myvar
.....
Return x
End Function

My question is: can i use this function in other aspx / code-behind pages
of my application? Or, till where is this function accessible?
thanks
Gilbert


Feb 25 '07 #3
Hi,

Scott M. wrote:
The function is only available while that page (class) is loaded, which is
only while the page is being requested.
Actually, a Page is an object like any other. "loaded" is a bit
confusing in this context.

When the request arrives, an instance of the corresponding Page is
created. The instance is disposed after the Response is sent. On the
next Request, another, brand new instance of the Page is created. This
explains why you cannot save instance variables over requests, but must
resort to Session, Cache, Application objects...

However, if you have a public method in a class deriving from Page, you
can use that method at any time simply by creating a new instance of
that Page:

MyOwnPage anInstance = new MyOwnPage();
anInstance.exec uteSomething();

However, I wouldn't recommend this. If you have a public method that is
used in multiple pages, extract this method to a different object and
use that object from all pages. It's all abut design ;-)

HTH,
Laurent

You could place a function like
this (along with other functions you wish to call repeatedly) in their own
assembly (.dll) and then just reference that assembly from your ASP.NET
project, make an instance of the class that contains them and run them as
you need to.
"Gilbert" <gi@bert.sdwrot e in message
news:Os******** ******@TK2MSFTN GP06.phx.gbl...
>Hi,

In the code-behind, i have this function:

Public Function myfunction(ByVa l myvar As Object) As String
dim x as string = myvar
.....
Return x
End Function

My question is: can i use this function in other aspx / code-behind pages
of my application? Or, till where is this function accessible?
thanks
Gilbert


--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 25 '07 #4
Hi,

Gilbert wrote:
Thanks,

What is then the meaning of 'Public'?. If the function would be 'Private',
would that change its reach?
It's not about the method being public or private. It's about the Page
instance's lifecycle. Read my other post in that thread.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 25 '07 #5
Thanks

"Laurent Bugnion [MVP]" <ga*********@bl uewin.chschreef in bericht
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Hi,

Scott M. wrote:
>The function is only available while that page (class) is loaded, which
is only while the page is being requested.

Actually, a Page is an object like any other. "loaded" is a bit confusing
in this context.

When the request arrives, an instance of the corresponding Page is
created. The instance is disposed after the Response is sent. On the next
Request, another, brand new instance of the Page is created. This explains
why you cannot save instance variables over requests, but must resort to
Session, Cache, Application objects...

However, if you have a public method in a class deriving from Page, you
can use that method at any time simply by creating a new instance of that
Page:

MyOwnPage anInstance = new MyOwnPage();
anInstance.exec uteSomething();

However, I wouldn't recommend this. If you have a public method that is
used in multiple pages, extract this method to a different object and use
that object from all pages. It's all abut design ;-)

HTH,
Laurent

>You could place a function like this (along with other functions you wish
to call repeatedly) in their own assembly (.dll) and then just reference
that assembly from your ASP.NET project, make an instance of the class
that contains them and run them as you need to.
"Gilbert" <gi@bert.sdwrot e in message
news:Os******* *******@TK2MSFT NGP06.phx.gbl.. .
>>Hi,

In the code-behind, i have this function:

Public Function myfunction(ByVa l myvar As Object) As String
dim x as string = myvar
.....
Return x
End Function

My question is: can i use this function in other aspx / code-behind
pages of my application? Or, till where is this function accessible?
thanks
Gilbert



--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Feb 25 '07 #6
I tried with 'Private' instead of 'Public' and this gives an error ("not
accessible in this context because it is 'Private")
With 'Public' or even with nothing, it works.

So, the answer on my question is:
when it's 'Private', the function is only accessible in the code-behind
page;
when 'Public' or nothing, it's also accessible from the aspx file.

Private Function myfunction(ByVa l myvar As Object) As String
.....
End Function

and in the aspx file:
------------------
<asp:Literal ID="rr" runat="server" Text='<%# myfunction(Eval ("field1")) %>'
/>




"Laurent Bugnion [MVP]" <ga*********@bl uewin.chschreef in bericht
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Hi,

Gilbert wrote:
>Thanks,

What is then the meaning of 'Public'?. If the function would be
'Private', would that change its reach?

It's not about the method being public or private. It's about the Page
instance's lifecycle. Read my other post in that thread.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Feb 25 '07 #7
Gee, I thought that is exactly what I said....

I indicated that a page is a class -
...while the page (class)...

And, loaded isn't really confusing at all, as I pointed out that this is
while "the page is being requested". Instances of classes are "loaded" into
the heap and assigned a pointer. While they have a pointer, they can be
accessed by your code. When the no longer have a pointer (like when the
page request is completed and the page response is loaded), they are the
GC's responsibility. They may still exist in memory (the heap) for some
period of time, but since the pointer to the object is gone, you can't get
to that particular instance of the object again.

Your last recommendation, "extract this method to a different object and use
that object from all pages" is exactly what I said, but more clearly, when I
indicated that the function could be placed inside of a class in a separate
..dll and then referenced and instantiated.

-Scott
"Laurent Bugnion [MVP]" <ga*********@bl uewin.chwrote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Hi,

Scott M. wrote:
>The function is only available while that page (class) is loaded, which
is only while the page is being requested.

Actually, a Page is an object like any other. "loaded" is a bit confusing
in this context.

When the request arrives, an instance of the corresponding Page is
created. The instance is disposed after the Response is sent. On the next
Request, another, brand new instance of the Page is created. This explains
why you cannot save instance variables over requests, but must resort to
Session, Cache, Application objects...

However, if you have a public method in a class deriving from Page, you
can use that method at any time simply by creating a new instance of that
Page:

MyOwnPage anInstance = new MyOwnPage();
anInstance.exec uteSomething();

However, I wouldn't recommend this. If you have a public method that is
used in multiple pages, extract this method to a different object and use
that object from all pages. It's all abut design ;-)

HTH,
Laurent

>You could place a function like this (along with other functions you wish
to call repeatedly) in their own assembly (.dll) and then just reference
that assembly from your ASP.NET project, make an instance of the class
that contains them and run them as you need to.
"Gilbert" <gi@bert.sdwrot e in message
news:Os******* *******@TK2MSFT NGP06.phx.gbl.. .
>>Hi,

In the code-behind, i have this function:

Public Function myfunction(ByVa l myvar As Object) As String
dim x as string = myvar
.....
Return x
End Function

My question is: can i use this function in other aspx / code-behind
pages of my application? Or, till where is this function accessible?
thanks
Gilbert



--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Feb 25 '07 #8
Hi,

Scott M. wrote:
Gee, I thought that is exactly what I said....
<snipYou're right. Somehow the first time I read your post, I
misunderstood what you meant; the fault lies on my side, not yours.
Your last recommendation, "extract this method to a different object and use
that object from all pages" is exactly what I said, but more clearly, when I
indicated that the function could be placed inside of a class in a separate
..dll and then referenced and instantiated.
IMHO the class doesn't need to be placed in a separate DLL, if it's used
only by different Pages in the same web application, it can be in a
separate object, but in the same DLL.
-Scott
Friendly greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Feb 25 '07 #9
When it's private, it's only accessible inside the same class. In this
case the class is the code behind. The code beind class inherits from
the Page class, and the class that is created for the aspx file inherits
from the code behind class.

To make members of the code behind class visible to the aspx class, you
can make them public or protected. Usually they are made protected as
they don't need to be more visible than that.

Not specifying accessibility at all should make them private.

Gilbert wrote:
I tried with 'Private' instead of 'Public' and this gives an error ("not
accessible in this context because it is 'Private")
With 'Public' or even with nothing, it works.

So, the answer on my question is:
when it's 'Private', the function is only accessible in the code-behind
page;
when 'Public' or nothing, it's also accessible from the aspx file.

Private Function myfunction(ByVa l myvar As Object) As String
....
End Function

and in the aspx file:
------------------
<asp:Literal ID="rr" runat="server" Text='<%# myfunction(Eval ("field1")) %>'
/>

"Laurent Bugnion [MVP]" <ga*********@bl uewin.chschreef in bericht
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
>Hi,

Gilbert wrote:
>>Thanks,

What is then the meaning of 'Public'?. If the function would be
'Private', would that change its reach?
It's not about the method being public or private. It's about the Page
instance's lifecycle. Read my other post in that thread.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


--
Göran Andersson
_____
http://www.guffa.com
Feb 25 '07 #10

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

Similar topics

21
4098
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
7
5541
by: Kevin | last post by:
Hi al I have an interesting question.... I am working witha Win API this is the Function Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Lon Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following ...
10
1533
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an object with that same ID already exists in the arraylist. If it does, I would like to increase the count of the matching object inside
5
1676
by: Colin McGuire | last post by:
Hi all, when I write the class below Private Class employee End Class and then add the line "Implements IVF" which is an interface I have written, the IDE modifies my code to display
3
1541
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address objects etc as Business layer classes and then have an data access layer which actually save / updates / delete to and from the DB.. If so, and bearing in mind the 'easiest' way to pass data between the layers would be with a dataset - should my BL...
3
7911
by: BoloBaby | last post by:
OK, I've managed to clarify my question (whew). I'll show two blocks of code - one in VB6.0 and one in VB.NET. The VB6.0 code manages to execute the callback function, the VB.NET does not. The question is "how do I get the .NET code to execute the callback function?" (Note - the declared function works to expose IRQs for a digital input/output card. Not relevant, but worth knowing if you were curious.) VB6.0 code:
7
1995
by: heltena | last post by:
Hi, I have this "interface" (abstract class): class Callback { public: virtual void function1() = 0; virtual void function2(string value) = 0; };
12
3034
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
3
1948
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee objects that I can iterate through relatively easily. I've included code at the bottom of this message. I can pretty easily iterate through my employee objects like so: Dim theEmployees As Employees = New Employees
4
237
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: this.TextBox2.TextChanged += new EventHandler(TextBox2_TextChanged); and then have the function. void TextBox2_TextChanged(object sender, EventArgs e)
0
9928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7110
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
5781
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
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.