Reputation: 1128
I have a long method that ends up returning a 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. The last line of code above calls a method back in my Form class which should update my progress bar. 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)'. If I set the importProgressBar method to static I get a bunch of other similar errors. Could anybody advise me how to fix this please?
Upvotes: 0
Views: 864
Reputation: 27962
frmImportCsvData
is a class, so a type, which is an abstract thing, not a real object in memory. 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
. 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:
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). 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. So grab a C# book and get back to the basics, that will help you the most.
Note: It is a common convention to start identifiers of methods, classes and properties with an upper-case letter.
Upvotes: 1