Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how can I share functions between multiple code behind pages ?

Posted on 2004-09-13
12
Medium Priority
?
491 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
[X]
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
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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1600 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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