views:

4652

answers:

6

Ok. I'm having an issue with the following bit of code:

StreamReader arrComputer = new StreamReader(FileDialog.FileName);

My first question had been answered already now my second question focuses on the tail end of this code.

I'm reading a text file StreamReader that the user selects with a button event using OpenFileDialog

private void button1_Click(object sender, EventArgs e)


        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.InitialDirectory = @"C:\";
            fileDialog.Filter = "Text|*.txt|All|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK) ;
            textBox1.Text = fileDialog.FileName;
            buttonRun.Enabled = true;
        }

The later in the code the user will click a "Run" button to exectue some code against each item in the list.

I'm having problems using StreamReader to parse the list using the following code:

private void buttonRun_Click(object sender, EventArgs e)
        {
            StreamReader arrComputer = new StreamReader(FileDialog.FileName);

        }

This is the error I receive:

An object reference is required for the non-static field, method, or property 'System.Windows.Forms.FileDialog.FileName.get'

I think I understand the problem but I'm having a hard time working it out.

A: 

Is FileDialog the name of your control, or the type of the control? I'm guessing it's the type. When you drag a file dialog into your window, you get a FileDialog named FileDialog1. Try that and let me know.

dnord
+2  A: 

Don't you think you need to use textBox1.Text?

 StreamReader arrComputer = new StreamReader(textBox1.Text);
shahkalpesh
+1  A: 

Try doing this instead:

private void buttonRun_Click(object sender, EventArgs e) {
    StreamReader arrComputer = new StreamReader(textBox1.Text);
}

When you OK your FileOpen dialog, you're storing the chosen filename on your form (by setting textBox1.Text), so you're better off using this stored value instead of referring back to the original FileOpen dialog.

Dylan Beattie
+3  A: 

Looks to me like you're creating a new OpenFileDialog object in your button1_Click method, and storing the only reference to that object in a local variable, fileDialog.

Then, in your buttonRun_Click method, it looks like you wanted to get the file name from the dialog you created in the previous method. That's not what you're doing, though. The compiler interprets your code as an attempt to read the FileName property of the FileDialog class as though it were a static member. There are other problems in your code, but the problem that's causing the compiler error you've cited is likely the FileDialog.FileName issue.

You mean to read the FileName property from the OpenFileDialog instance you created in the first method, but that object is only stored in a local variable. You have no access to it outside that first method. But since you've also stored the file name in the text box, you can read the file name out of that text box, so you don't need access to the OpenFileDialog object.

Rob Kennedy
Excellent explanation. Thank you. I changed it to StreamReader arrComputer = new StreamReader(textBox1.Text) and that worked. Newbie mistake.
Jim
Not exactly a newbie mistake, just once you have done it before, you fix it before anyone notices :)
Nat
+2  A: 

FileDialog is a class name, and you need to use an object to access the FileName property, hence the error. I'd recommend using fileDialog.FileName, but you've already thrown away your fileDialog (note the lowercase "f") when the button1_Clickmethod exited.

However, you saved the file name in textBox1.Text before that method exited, and it should still be available. Try using that:

StreamReader arrComputer = new StreamReader(textBox1.Text);
Blair Conrad
A: 

In button1_Click you defined a local fileDialog variable which disappears at the end of the event handler.

In buttonRun_Click you are using a class method on the class FileDialog.

It seems that you need to declare a fileDialog variable at the form level (outside button1_Click) and use the same variable in both event handlers.

When doing this watch for the fileDialog and FileDialog spelling.

hectorsq