我该怎么做才能修复错误 [英] what can i do to fix error

查看:91
本文介绍了我该怎么做才能修复错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonIndex_Click_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";
        }


    }
}









错误:错误1当前上下文中不存在名称'openFileDialog'





error :Error 1 The name 'openFileDialog' does not exist in the current context

推荐答案

现在你做得更好。我看到你启动了Windows窗体应用程序。

代码不起作用,因为你需要声明和实例化openFileDialog对象。



最简单的方法是将OpenFileDialog从工具箱拖到表单中(您也可以双击)。然后你必须将它重命名为你的代码所期望的(openFileDialog)。这应该解决它。它应该编译。
Now you are doing better. I see you started Windows Forms application.
Code doesn't work because you need to have openFileDialog object declared and instantiated.

Easiest way is to drag OpenFileDialog from the toolbox to your form (you can also double click). Then you have to rename it to what your code is expecting (openFileDialog). And this should solve it. It should compile.


只是复制代码并将其粘贴到你的项目中是行不通的,除非你理解代码是什么以及它是如何工作的!



问题是你没有在表单设计器中将OpenFileDialog拖放到表单上。然后,您必须在属性窗口中将其名称更改为openFileDialog。



但是,除此之外,我还会看到您遇到的其他问题。 Serisouly,拿起一本关于C#的初学者书并完成它。如果您要继续编写代码,它会为您节省大量时间来猜测您正在做什么,并教您一些基本的调试技巧。
Just copying code and pasting it into your project isn't going to work unless YOU UNDERSTAND WHAT THAT CODE DOES AND HOW IT WORKS!

The problem is you didn't drag and drop an OpenFileDialog onto your Form in the Form Designer. You then have to change it's name to "openFileDialog" in the Properties window.

But, beyond that, I see other problems you're going to run into. Serisouly, pick up a beginners book on C# and work through it. It'll save you a ton of time guessing at what you're doing and teach you some basic debugging skills you WILL need if you're going to continue writing code.


您正在使用一个OpenFileDialong:



1.未在任何地方创建。







2.创建的范围与您尝试使用的范围不同,如果表单上尚不存在。
You are using an OpenFileDialong that:

1. Has not been created anywhere.

or

2. Was created in a different scope than you are attempting to use it in, if it does not already exist on the form.


这篇关于我该怎么做才能修复错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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