[英]An object reference is required for the non-static field error
I have a long method that ends up returning a DataTable. 我有一个很长的方法,最终返回一个DataTable。 The method opens like this:
方法打开如下:
public DataTable ImportCsvData(string folderPath, string[] csvFileNames, int compIdValue)
{
int countDupFiles = 0;// Count number of previously imported csv files
int countImportedFiles = 0;// Count imported files
for (int i = 0; i < csvFileNames.Length; i++)
{
frmImportCsvData.importProgressBar(i);
This method is inside a class where I am doing all of my SQL requests but is called from another class that controls my Form. 此方法在我正在执行所有SQL请求的类中,但是从另一个控制我的Form的类中调用。 The last line of code above calls a method back in my Form class which should update my progress bar.
上面的最后一行代码在Form类中调用了一个方法,该方法应该更新进度条。 The code for that method is:
该方法的代码是:
public void importProgressBar(int i)
{
progressTableLayoutPanel.Visible = true;//display progress bar
int percProgress = 100 * (i + 1) / csvFileNames.Length;
while (percProgress < 100)
{
if (percProgress <= 99)// Required to prevent values above 100 that crash the code
progressBar.Value = percProgress + 1;//hack that makes the progress bar update when progress value decreases
progressBar.Value = percProgress;
percProgressLabel.Text = percProgress.ToString();
progressTableLayoutPanel.Update();//Required to display all progress bar table contents
//Thread.Sleep(200);
}
progressTableLayoutPanel.Visible = false;
}
This gives me the error: An object reference is required for the non-static field, method, or property 'BetfairDatabaseMDI.frmImportCsvData.importProgressBar(int)'. 这给了我错误:非静态字段,方法或属性'BetfairDatabaseMDI.frmImportCsvData.importProgressBar(int)'需要对象引用。 If I set the importProgressBar method to static I get a bunch of other similar errors.
如果我将importProgressBar方法设置为static,则会收到许多其他类似错误。 Could anybody advise me how to fix this please?
有人可以建议我如何解决此问题吗?
frmImportCsvData
is a class, so a type , which is an abstract thing, not a real object in memory. frmImportCsvData
是一个类,所以type是一个抽象的东西,而不是内存中的真实对象。 The error message says you need an instance of a type, so a concrete, existing thing, an object in memory of that given type . 错误消息指出您需要一个类型的实例 ,因此需要一个具体的现有事物,该给定类型的内存中的对象。
So, essentially, you need to pass the instance of frmImportCsvData
into ImportCsvData
. 因此,从本质
frmImportCsvData
,您需要将frmImportCsvData
的实例 frmImportCsvData
给ImportCsvData
。 For example, like this: 例如,像这样:
public DataTable ImportCsvData(frmImportCsvData myForm, …)
{
myForm.importProgressBar(i);
}
Then to call the method from within frmImportCsvData
you just pass in this
, which denotes the current instance (in run-time sense) of the enclosing class: 然后从
frmImportCsvData
内部调用该方法,您只需传入this
,它表示封闭类的当前实例(在运行时):
dataLayer.ImportCsvData(this, …);
Simplest doesn't mean best or correct in respect to the particular UI framework you are using (being it WinForms, WPF, whatever). 对于您正在使用的特定UI框架(无论是WinForms,WPF,还是其他),最简单并不意味着最佳或正确。 BUT: You definitelly have to understand the differences between a type and an instance of a type first , before you run into any more complex constructs.
但是:你definitelly必须先了解一个类型和类型的实例之间的差异,您遇到任何更复杂的结构之前。 So grab a C# book and get back to the basics, that will help you the most.
因此,抓住一本C#书籍并回到基础知识,对您有最大的帮助。
Note: It is a common convention to start identifiers of methods, classes and properties with an upper-case letter. 注意:以大写字母开头的方法,类和属性的标识符是一种常见的约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.