非靜態字段,方法或屬性需要對象引用

[英]An object reference is required for the non-static field, method, or property


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

我正在閱讀一個文本文件StreamReader,用戶使用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 execute some code against each item in the list.

在代碼中的后面,用戶將單擊“運行”按鈕以對列表中的每個項執行一些代碼。

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

我使用StreamReader使用以下代碼解析列表時遇到問題:

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

        }

This is the error I receive 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'm having a hard time working it out.

我想我理解這個問題,但我很難解決問題。

6 个解决方案

#1


4  

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.

在我看來,就像你在button1_Click方法中創建一個新的OpenFileDialog對象,並在該局部變量fi​​leDialog中存儲對該對象的唯一引用。

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.

然后,在buttonRun_Click方法中,看起來您想從上一個方法中創建的對話框中獲取文件名。但這不是你正在做的事情。編譯器將您的代碼解釋為嘗試讀取FileDialog類的FileName屬性,就像它是靜態成員一樣。您的代碼中還有其他問題,但導致您引用的編譯器錯誤的問題可能是FileDialog.FileName問題。

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.

您的意思是從您在第一個方法中創建的OpenFileDialog實例中讀取FileName屬性,但該對象僅存儲在本地變量中。在第一種方法之外,您無權訪問它。但是,由於您還在文本框中存儲了文件名,因此可以從該文本框中讀取文件名,因此您無需訪問OpenFileDialog對象。

#2


2  

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.

FileDialog是一個類名,您需要使用一個對象來訪問FileName屬性,因此錯誤。我建議使用fileDialog.FileName,但是當button1_Click方法退出時,你已經拋棄了你的fileDialog(注意小寫的“f”)。

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

但是,您在退出該方法之前將文件名保存在textBox1.Text中,並且它仍然可用。嘗試使用:

StreamReader arrComputer = new StreamReader(textBox1.Text); 

#3


2  

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

您認為不需要使用textBox1.Text嗎?

 StreamReader arrComputer = new StreamReader(textBox1.Text);

#4


1  

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.

當您確定FileOpen對話框時,您將在窗體上存儲所選的文件名(通過設置textBox1.Text),因此最好使用此存儲的值而不是返回原始的FileOpen對話框。

#5


0  

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.

FileDialog是控件的名稱,還是控件的類型?我猜這是類型。將文件對話框拖到窗口中時,會得到一個名為FileDialog1的FileDialog。試試看,讓我知道。

#6


0  

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

在button1_Click中,您定義了一個本地fileDialog變量,該變量在事件處理程序的末尾消失。

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

在buttonRun_Click中,您在類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.

您似乎需要在表單級別(button1_Click外部)聲明fileDialog變量,並在兩個事件處理程序中使用相同的變量。

When doing this watch for the fileDialog and FileDialog spelling.

這樣做時會監視fileDialog和FileDialog的拼寫。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/11/10/72080d463b843aa6c412cf8e4bd2cbde.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com