我得到这些错误,为什么? [英] i get these error , why?

查看:69
本文介绍了我得到这些错误,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        // index files
        private void buttonIndex_Click(object sender, EventArgs e)
        {

            if (openFileDialog.ShowDialog() != DialogResult.OK)
                return;
            
            // parse each file and build the inverted index. 
            foreach (string fileName in openFileDialog.FileNames)
            {
                string text = File.ReadAllText(fileName).Replace("\r", " ").Replace("\n", " ");
                string[] terms = text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                // parse each term (word) in text file and put it in dictionary
                foreach (string term in terms)
                {
                    if (!InvertedIndex.ContainsKey(term))
                        InvertedIndex.Add(term, new List<string>());

                    if (!InvertedIndex[term].Contains(fileName))
                        InvertedIndex[term].Add(fileName);
                }
            }

            // update label
            labelIndex.Text = openFileDialog.FileNames.Length.ToString() + " files indexed";
        }


        // perform a seach over the inverted index built earlier
        private void buttonSearch_Click(object sender, EventArgs e)
        {

            // split query in terms
            string query = textBoxQuery.Text;
            string[] terms = query.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);


            // see documents for each term and merge them
            List<string> commonDocuments = null;
            foreach (string term in terms)
            {
                // if one term not in index, end search
                if (!InvertedIndex.ContainsKey(term))
                {
                    if (commonDocuments != null)
                        commonDocuments.Clear();
                    break;
                }

                // find documents containing all terms
                if (commonDocuments == null)
                    commonDocuments = InvertedIndex[term];
                else
                    commonDocuments = GetCommonDocuments(commonDocuments, InvertedIndex[term]);
            }


            // display results
            if (commonDocuments != null)
            {
                listBoxResults.Items.Clear();
                foreach (string fileName in commonDocuments)
                    listBoxResults.Items.Add(fileName);
            }
        }




        // merge two arrays of file indexes
        private List<string> GetCommonDocuments(List<string> documentList, List<string> newDocumentsList)
        {
            List<string> common = new List<string>();
            foreach (string file in documentList)
                if (newDocumentsList.Contains(file))
                    common.Add(file);

            return common;
        }


    }
}







错误:






Error:

Error   1   The name 'openFileDialog' does not exist in the current 
Error   2   The name 'DialogResult' does not exist in the current context   
Error   3   The name 'openFileDialog' does not exist in the current context 
Error   4   The name 'labelIndex' does not exist in the current context 
Error   5   The name 'openFileDialog' does not exist in the current context 
Error   6   The name 'textBoxQuery' does not exist in the current context   
Error   7   The name 'InvertedIndex' does not exist in the current context  
Error   8   The name 'InvertedIndex' does not exist in the current context  
Error   9   The name 'InvertedIndex' does not exist in the current context  
Error   10  The name 'listBoxResults' does not exist in the current context 
Error   11  The name 'listBoxResults' does not exist in the current context

推荐答案

为什么?因为您从其他人的代码中复制粘贴了这些代码行,他们了解他/她所做的事情。你不明白。此代码段是Windows窗体应用程序的一部分。您不能将其粘贴到控制台应用程序中。以下是告诉我的:

Why? Because you copy-pasted this code rows from someone else's code, who understood what he/she did. You don't understand. This code snippet is part of a Windows forms application. You can't just paste it into a console application. Here is what's telling me that:
namespace ConsoleApplication3
{
    class Program
    {



您可以从控制台应用程序(如对话框)调用某些表单功能,但您将无法解析标签和lisboxes。

您可能不需要所有这些,但是为了能够摆脱它并使代码在控制台应用程序中工作,您需要了解它,并根据需要进行重构。如果可能的话。但你最好从头开始。


You can call some of forms features from a console application (like dialogs), but you won't be able to resolve the labels and lisboxes.
You might not need all this, but to be able to get rid of it and get the code working in a console application you need to understand it, and refactor it as needed. If possible at all. But you better start from the scratch.


为什么?

因为你的源代码中没有所需的命名空间:在本例中是System.Windows。表格。



将输入光标移动到编译器抱怨的类名中。

一条小蓝线将出现在这个名字。

将鼠标悬停在线上,然后会出现一个下拉列表。打开下拉菜单。

您将获得帮助解决问题的选项。在这种情况下,选择第一个选项,将使用语句添加到文件顶部。



对它仍在抱怨的所有名称重复此操作。
Why?
Because you don't have the needed namespaces included in your source: In this case System.Windows.Forms.

Move the input cursor into the class name the compiler is complaining about.
A small blue line will appear at the beginning of the name.
Hover your mouse over the line, and a drop down will appear. Open the drop down.
You will be given options to help fix the problem. In this case, select the first option which will add the using statement to the top of your file for you.

Repeat this for all the names it is still complaining about.


1。 System.Windows.Forms命名空间中提供了OpenFileDialog类。因此,您需要在代码中添加以下行。

1. OpenFileDialog Class is available in System.Windows.Forms namespace. So, you need to add below line in your code.
using System.Windows.Forms;





如果仍然出现错误,则可能需要添加对System.Wondows.Forms.dll的引用,这可以在此步骤之后完成:右键单击解决方案资源管理器中的引用文件夹。在添加参考对话框的.NET选项卡中,搜索​​System.Windows.Forms,然后单击确定按钮。



2.第二个错误也应通过以下方式解决:按照上述步骤进行操作。



3.对于您提到的第3至第11错误,即将发生,因为您尚未声明此类名称的任何对象。比如解决第3个错误,你需要创建一个名为openFileDialog的OpenFileDialog对象,如下所示。



If you are still getting error, then you might need to add reference to System.Wondows.Forms.dll, which can be done following this step: Right Click on Reference folder in Solution Explorer. In .NET tab of the Add Referrence Dialog box, Search for System.Windows.Forms and than Click on ok button.

2. Second Error should also get resolved by following above steps.

3. For Error no 3rd to 11th mentioned by you, is coming because you have not declared any object of such names. like for resolving error no 3, you need to create an object named openFileDialog of class OpenFileDialog as below.

OpenFileDialog openFileDialog = new OpenFileDialog();





希望这会有所帮助。 !快乐编程!!



Hope this will help. !! Happy Programming !!


这篇关于我得到这些错误,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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