Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 17-Aug-11 18:21pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use Static class[^], you can put all your misc function in there.
  Permalink  
Comments
Karthik. A at 18-Aug-11 0:05am
   
This is also a good option, apart from my answer...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
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
 
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.
  Permalink  
v2
Comments
Mohammad A Rahman at 18-Aug-11 0:06am
   
I agree, you are just too quick , 5 :)
Karthik. A at 18-Aug-11 0:12am
   
thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It might help you,
 
Inheritance[^]
 
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Excelent, implemented in my project
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 29 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100