3

我收到上述错误。我在这个错误上看到了其他类似的帖子,但我似乎没有找到问题所在。我将我的类类型更改为静态并且错误抵抗。这是我的主表单类的完整代码:

我编辑了我的课程。这就是现在的样子,错误已更改为:“PhoneFind.frmMain”类型声明时缺少部分修饰符;这种类型的另一个部分声明存在。

namespace PhoneFind
{
    public partial class frmMain : Form
    {
        // the path to the temporary file to store the webresponse
        String path = "c:\\Temp\\webresponse.txt";
        public frmMain()
        {
            InitializeComponent();
            comboSelectSearchEngine.SelectedIndex = 0;
            ofdPhones.Filter = "txt files (*.txt)|*.txt";
            listbxMessages.Items.Clear();
            addItemToListBox(listbxMessages, "Welcome to ActionBase Phone Find!");
            addItemToListBox(listbxMessages, "Select the file containing the numbers.");
            radioNumbers.Checked = true;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            if (ofdPhones.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFile.Text = ofdPhones.FileName;

                // Read the file line by line and add the numbers to the numbers listbox.
                listbxNumbers.Items.Clear();
                String line;
                int numbersCounter = 0;
                System.IO.StreamReader file = new System.IO.StreamReader(ofdPhones.FileName);
                while ((line = file.ReadLine()) != null)
                {
                    listbxNumbers.Items.Add(line.Trim());
                    numbersCounter++;
                }
                addItemToListBox(listbxMessages, ofdPhones.FileName + " loaded.");
                addItemToListBox(listbxMessages, numbersCounter + " records found in the file.");

            }
        }

        // add item to the listbox and move scroll to the end of the listbox for the latest messages to be visibile to the viewer
        private void addItemToListBox(ListBox listbox, String item)
        {
            listbox.Items.Add(item);
            listbox.SelectedIndex = (listbox.Items.Count - 1);
        }

        private void radioNumbers_CheckedChanged(object sender, EventArgs e)
        {
            if (!radioNumbers.Checked)
            {
                grpbxNumbers.Text = "Names and Addresses";
            }
            else
            {
                grpbxNumbers.Text = "Numbers";
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (listbxNumbers.Items.Count == 0)
            {
                MessageBox.Show("No records have been loaded." + "\n" + "Use the browse button to load records.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!CheckForInternetConnection())
            {
                MessageBox.Show("No internet connection.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String response;
                switch (comboSelectSearchEngine.SelectedIndex)
                {
                    case 0: // Hitta.se
                        foreach (String item in listbxNumbers.Items)
                        {
                            WebRequestObj request = new WebRequestObj(item, "hitta");
                            response = request.sendRequest();
                            if (response.Equals("Error"))
                            {
                                addItemToListBox(listbxMessages, "Error sending '" + item + "' to the server.");
                            }
                            else
                            {
                                //create a temporary file to work on the response
                                StreamWriter sw;
                                if (!File.Exists(path)) {
                                    sw = File.CreateText(path);
                                }
                                try
                                {
                                    File.WriteAllText(path, String.Empty); // clear the content of the file
                                    sw = File.AppendText(path);
                                    sw.WriteLine(response);
                                    sw.Flush();
                                    sw.Close();
                                    String s = findResultType(path);
                                    MessageBox.Show(s);
                                }
                                catch (IOException ioe)
                                {
                                    MessageBox.Show(ioe.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }


                            }
                        }
                        break;
                }
            }
        }

        public bool CheckForInternetConnection()
        {
            try
            {
                using (var client = new System.Net.WebClient())
                using (var stream = client.OpenRead("http://www.google.com"))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /*
         * findResultType
         * gets the web response and finds out if the matching result of the search termis one of the following:
         * 1. one person (oneperson)
         * 2. one company (onecompany)
         * 3. more than one person, no company (manypersonnocompany)
         * 4. no person, more than one company (nopersonmanycompany)
         * 5. more than one person, more than one company (manypersonmanycompany)
         * 6. no person, no company (nopersonnocompany)
         */
        public String findResultType(String reponsepath)
        {                   
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(reponsepath);
            List<String> itemList = new List<String>();
            IEnumerable<String> v = null;
            var item = doc.DocumentNode.SelectNodes("//body[@id='person']");

            if (item != null)
            {
                v = item.Select(p => p.InnerText);
                itemList = v.ToList();
                if (itemList.Count == 1)
                    return "oneperson";
            }
            else
            {
                item = doc.DocumentNode.SelectNodes("//body[@id='company']");
                if (item != null)
                {
                    v = item.Select(p => p.InnerText);
                    itemList = v.ToList();
                    if (itemList.Count == 1)
                        return "onecompany";
                }
            }

            //for (int i = 0; i < itemList.Count; i++)
            //{
            //    MessageBox.Show(itemList[i]);
            //    //console.writeline(itemlist[i]);
            //}
            //console.writeline(itemlist.count + " results found.");
            return "";
        }
    }
}
4

3 回答 3

11

这是您的第一个问题:

public static class frmMain : Form

您不能从另一个类派生静态类。静态类总是隐式派生自Object. 静态类中也不能有非静态成员。

所以基本上,你不能把你的扩展方法放在你的frmMain类中(应该重命名以遵循 .NET 命名约定并同时更具描述性)。你为什么要把扩展方法放在那个类中呢?

我什至在您发布的代码中都看不到扩展方法 - 您是否删除了它?是在您尚未发布的其他课程中吗?

从根本上说,我认为你需要退后一步。听起来您在没有完全理解编译器错误消息的情况下做出了反应。阅读扩展方法,阅读什么是静态类,直到你真正理解为什么你不希望你的表单类是静态的,以及为什么你想让包含你的扩展方法的类成为静态的。

于 2012-11-07T16:07:33.340 回答
1

一个static类不能有公共构造函数。此外,static表单毫无意义,因为您需要显示它们的实例。

如果你有一个扩展方法(我没有看到任何......),你必须将它移动到一个单独的static类中,你不能创建以下实例:

static class MyExtensions
{
  public static void Extend(this Object o) {}
}
于 2012-11-07T16:10:10.827 回答
0

您应该始终将扩展方法放在单独的静态类中。

您无法通过简单地将类标记为静态来解决问题。这改变了类的整个含义。相反,为扩展方法创建一个新类。

例如:

public static class Extensions
{
    public void SomeExtension(this object arg)
    {
        ...
    }
}
于 2012-11-07T16:10:53.097 回答