An object reference is required for a non-static field, method or property

Ok. I am having a problem with the following code:

StreamReader arrComputer = new StreamReader(FileDialog.FileName);

      

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

I am reading a text file StreamReader

that a user selects via a button event usingOpenFileDialog

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;
        }

      

Later in the code, the user clicks the Run button to execute the code on each item in the list.

I am having problems using StreamReader to parse a list with the following code:

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

        }

      

This is the error I am getting from my coding:

"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 am having a hard time dealing with it.

0


source to share


6 answers


It looks to me like you are creating a new OpenFileDialog object in your button1_Click method and storing the only reference to that object in the local fileDialog variable.

Then, in your buttonRun_Click method, it looks like you wanted to get the filename from the dialog created in the previous method. However, this is not what you are doing. The compiler interprets your code as trying to read the FileName property of the FileDialog class as if it were a static member. There are other issues in your code, but the problem that caused the compiler error is most likely an issue with FileDialog.FileName.



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

+4


source


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



 StreamReader arrComputer = new StreamReader(textBox1.Text);

      

+2


source


FileDialog

is the name of the class and you need to use an object to access the property FileName

hence the error. I would recommend using fileDialog.FileName

, but you already selected yours FileDialog

(note the lowercase "f") when the method exited button1_Click

.

However, you saved the filename in textBox1.Text

before this method exited and should still be available. Try using this:

StreamReader arrComputer = new StreamReader(textBox1.Text); 

      

+2


source


Try this instead:

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

      

When you're ok in the FileOpen dialog, you store the selected filename in your form (by setting textBox1.Text), so you'd better use that stored value instead of referring to the original FileOpen dialog.

+1


source


Is the FileDialog

name of your control or the type of control? I am assuming this is the type. When you drag a file dialog into your window, you get a FileDialog named FileDialog1. Try this and let me know.

0


source


In button1_Click

you specified a local variable fileDialog

that disappears at the end of the event handler.

In buttonRun_Click

you are using a class method for a class fileDialog

.

You seem to need to declare a fileDialog variable at the form level (outside of button1_Click) and use the same variable in both event handlers.

When doing this clock for spelling fileDialog

and fileDialog

.

0


source







All Articles