Click here to Skip to main content
15,914,416 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
C#
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";
        }


    }
}




error :Error 1 The name 'openFileDialog' does not exist in the current context
Posted
Updated 16-Dec-13 9:39am
v2
Comments
joshrduncan2012 16-Dec-13 15:42pm    
Just a hunch, but you probably haven't declared your openfiledialog in this line:

if (openFileDialog.ShowDialog() != DialogResult.OK)
return;

You are probably using a variable that doesn't exist. Unless you name it something different, if you use the designer the default name is openFileDialog1. If you declare it in code, then use what you declared.

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.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900