类之间的c#/asp.net共享方法 [英] c#/asp.net sharing methods between classes

查看:77
本文介绍了类之间的c#/asp.net共享方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.
我正在制作一个项目,需要在更多页面中使用一种方法.

我有3页,每页上我必须调用一个名为Foo(string s)的方法,该方法将返回一个int32值.
现在,我在每个页面中都编写了相同的方法(复制/粘贴),但是我想要更干净的东西,而又不浪费时间和代码.

我只能写一次方法Foo(string s),并在所有页面中使用它吗?能给我一个简单的例子吗?

在此先感谢!

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!

推荐答案

使用 ^ ],您可以在其中放置所有杂项功能.
Use Static class[^], you can put all your misc function in there.


这是我脑海中浮现的一种简单方法

1.创建一个将被所有3页使用的基类

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.您的所有3页都应该继承此页面,而不是System.Web.UI.Page



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.


它可能对您有帮助,

继承 [
It might help you,

Inheritance[^]

:)


这篇关于类之间的c#/asp.net共享方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆