Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how can I share functions between multiple code behind pages ?

Posted on 2004-09-13
12
488 Views
Last Modified: 2012-08-13
Hi There,

I am wondering how I can share common functions between code behind pages? I have a few codebehind pages that use common sub routines / functions and I want to make the available to all codebehind pages but I only have to change the code in one place.

Sean
0
Comment
Question by:bullrout
12 Comments
 
LVL 6

Expert Comment

by:Volkan Vardar
ID: 12042342
add a public class to project.
or a module.
like


Public Class clsCommon
    Public Shared Function myfunc() As String
        'bla bla bla
    End Function
End Class
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12042348
Hi,

you can create a common class ( create new item in your project of type class ) and you can use it to define functions/methods in it that will be called from the other classes in the project


Regards,
B..M
0
 
LVL 1

Accepted Solution

by:
vinayakrishna earned 100 total points
ID: 12042406
You can add a Class file with all the commonly used functions in it,to your solution.

Then you can create an object of this class and access all functions defined in it in all your .cs files.Like this :

ProductController prodcontroller = new ProductController();

prodcontroller.Function1();
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Author Comment

by:bullrout
ID: 12042904
Hi There,

So if I have a .vb file in my dir called common and then I can call functions inside that class file by creating an instance of that class inside another codebehind without actually having a reference to it (such as a file path)?

I tried to do something like below, where I have listbox that is populated from a sql server db, I'm not sure how to declare the class inside the code behind page in order to use it?

Sorry for all of the questions, I am making the transition from asp to asp .net and I not used to oop yet.

Sean

Public Class CommonFunctions

 Public Shared Function myfunc() As String
        'access the db here
    End Function

End class

!--- in a different code behind page

Public Class DoSomething

        lst1.DataSource = commonfunctions.myfunc()
        lst1.DataValueField = "fieldname"
        lst1.DataTextField = "fieldname"
        lst1.DataBind()
        lst1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("pick one","0"))
        lst1.SelectedIndex = 0      

End class


0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12043055
you may need a new in there like

lst1.DataSource = New commonfunctions.myfunc()

but other than that looks good

Regards,

Aeros
0
 
LVL 4

Assisted Solution

by:itcnbwise
itcnbwise earned 400 total points
ID: 12044845
If you work with the code (like me) and compile it without using Visual Studio, here's what you'd do:

Namespace commonIncludes

  Public Class CommonFunctions

    Public Shared Function myfunc() As String
        'access the db here
    End Function

    'mo functions, mo functions, mo functions ...

End class

End Namespace

**************

Then, in any page you want to use that function, import it:

Imports System ...
Imports commonIncludes

Public Class thisIsMyPage: Inherits System.Web.UI.Page

  Public myIncludes As New CommonFunctions

  Sub Page_Load(sender as Object, e as EventArgs)
    Dim thisVar As String = myIncludes.myfunc()
  End Sub

End Class
0
 
LVL 4

Expert Comment

by:itcnbwise
ID: 12044857
FYI -  of course, that work's with Visual Studio as well, I just wanted to show you all the code needed.  VS tends to hide the more complicated stuff so you don't always understand what it's doing.
0
 
LVL 5

Author Comment

by:bullrout
ID: 12050052
Hi itcnbwise,

I have not compiled any of my code, I'm just referencing a codebehind page via a path, is this the wrong way to do it?

Sean
0
 
LVL 4

Expert Comment

by:itcnbwise
ID: 12050077
Well, there's no wrong way to do it, but there is a better way :)  Pre-compiled pages are stored as DLLs in the /bin/ directory of your web server.  If your page isn't pre-compiled (i.e., the codebehind is just a VB file) then the webserver has to compile it on-the-fly the first time it's requested after a source code change.  Not a big deal, unless you're the first user to request the file :)

In short, you don't need to compile your pages, but there is a performance gain if you do.   Regardless, you should still be able to access an included file.  As I said, I use the free vbc.exe file that comes with .NET to compile all my code on the command line, but you can use Visual Studio if you want to give it a try.
0
 
LVL 5

Author Comment

by:bullrout
ID: 12050095
Hi itcnbwise,

do you have any commandline syntax examples for this? (vbc.exe) and can this be done without a visual studio project file?

Sean
0
 
LVL 4

Expert Comment

by:itcnbwise
ID: 12050158
Sure, here's how you'd compile a page on the command line:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc /nologo /t:library /out:..\bin\YOURFILE.dll /r:System.dll /r:System.Web.dll /r:System.Data.dll /r:System.Xml.dll /r:..\bin\YOURINCLUDESFILE.dll  YOURFILE.aspx.vb

Notice you have to include any libraries you are using into the compile.  You would use a command like this to first compile YOURINCLUDESFILE.dll , then tun this to compile YOURFILE.aspx.vb.

And yes, this can definitely be done with VS I just don't know the steps, because I never use it.  But I have an open source project which explains all this, has the command line compile options in BAT files, and it uses an include file.  Go to http://forum.itcn.com/ - it's a free, open source project I created for newcomers to learn ASP.NET.  Take a look at the source code for more ideas.
0
 
LVL 5

Author Comment

by:bullrout
ID: 12061261
Hi All,

Thanks for all the help, I have made my application into a project and this seems to be making my life easier.

Sean
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question