如何将我的方法移出主代码文件

时间:2012-12-31 11:40:23

标签: c# class methods

我有一个程序正在从网络生成一些统计数据,但现在我最终在我的主文件中有30多个方法,我想将我的方法拆分到其他文件,这样我可以更好地维护它们和主程序会很可读的。 (我是c#的新手,我不知道我怎么能这样做,我需要一些参考)

我的方法就像这些。

1)字符串方法

cut_string(string text)

find_string(string text)

2)网络方法

get_url_source(string url)

3)特定网络方法1

...

4)特定网络方法2

...

现在我的主程序正在搜索一系列页面并生成一些数据。 示例myweb.com/1-100/

using imports...

namespace PirateBot
{
    public partial class MyProgram : Form
    {
        private void My_Button(object sender, EventArgs e)
        {
            start_program();
        }

        private void start_program()
        {
            for (int i = 1 ; i =<100 ; i++)
            {
                //use web methods
                //use string methods
                //etc
            }
        }
        //my methods...
    }
}

1 个答案:

答案 0 :(得分:1)

您需要Extract Class重构。

将相似/相关的方法组合在一起并转移到不同的类别。

示例(以下可以是您的新班级)

class StringHelper
{
  // String specific methods
}  

class WebHelper
{
  // String specific methods
}  

...