Click here to Skip to main content
15,507,266 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello.
I'm making a project where I need to use a method in more pages.

I have 3 pages, on each one I must call a method named Foo(string s), this method will return a int32 value.

Now I written the same method (copy/paste) in each page, but I want something more clean, without wasting time and code.

Could I write only once the method Foo(string s), and use it in all pages? Can you give me a simple example?

Thanks in advance!
Posted

Use Static class[^], you can put all your misc function in there.
 
Share this answer
 
Comments
Karthik. A 18-Aug-11 0:05am    
This is also a good option, apart from my answer...
This is a simple way that immediately comes to my mind

1. Create a base class that will be used by all the 3 pages

C#
public class BasePage : System.Web.UI.Page
{
    public int YourMethod(string str)
    {
        int someNum = 1;
        return someNum;
    }
}


2. All your 3 pages should inherit this, instead of System.Web.UI.Page

C#
public class Default : BasePage
{
      protected void Page_Load(object sender, EventArgs e)
      {
          // You can use YourMethod here
      }
}


Hope this helps! Comment if you have any questions.
 
Share this answer
 
v2
Comments
Mohammad A Rahman 18-Aug-11 0:06am    
I agree, you are just too quick , 5 :)
Karthik. A 18-Aug-11 0:12am    
thanks!
It might help you,

Inheritance[^]

:)
 
Share this answer
 
Excelent, implemented in my project
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900