C# 非静态字段、方法或属性需要对象引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/279306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An object reference is required for the non-static field, method, or property
提问by Jim
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.
我想我明白这个问题,但我很难解决这个问题。
采纳答案by Rob Kennedy
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 对象,并将对该对象的唯一引用存储在局部变量 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.
然后,在您的 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 对象。
回答by dnord
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。试试看,让我知道。
回答by shahkalpesh
Don't you think you need to use textBox1.Text?
你不认为你需要使用 textBox1.Text 吗?
StreamReader arrComputer = new StreamReader(textBox1.Text);
回答by Dylan Beattie
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 对话框。
回答by Blair Conrad
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_Click
method exited.
FileDialog
是一个类名,您需要使用一个对象来访问该FileName
属性,因此出现错误。我建议使用fileDialog.FileName
,但是fileDialog
当button1_Click
方法退出时你已经扔掉了你的(注意小写的“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);
回答by hectorsq
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
拼写。