0

I am new to c# and I have a trouble: Error CS0120 An object reference is required for the nonstatic field, method, or property In russian it is little bit different: Error CS0120 For non-static field, method, or property is required link to an object.

When I'm closing one form, that downloads some staff and saves it to a .xml files, I should update a combobox on the another form (This form contains a list of recently downloaded files.)

But I have an error. How can I fix it?

The code:

private void Reviews_browser_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Determine if text has changed in the textbox by comparing to original text.

        // Display a MsgBox asking the user to save changes or abort.
        if (MessageBox.Show("Закрыть, сохранив загруженные БД?", "Внимание!",
           MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            // Cancel the Closing event from closing the form.
            e.Cancel = false;
            // Call method to save file...
        }
        else
        {
            e.Cancel = true;
            try
            {
                string[] dirs = Directory.GetFiles(@Directory.GetCurrentDirectory(), "*.xml");
                foreach (string dir in dirs)
                {
                    if (Path.GetFileName(dir) != "HtmlAgilityPack.xml" && Path.GetFileName(dir) != ".xml")
                    {
                        int len = Path.GetFileName(dir).Length;
                        glavnaya.fayly_s_otzivami.Items.Add(Path.GetFileName(dir).Remove(len - 4, 4));
                    }
                }
            }
            catch (Exception oshibocka)
            {

            }
        }
  • 1
    What is and where is declared the _glavnaya_ variable? Is it a reference to the form where the (I suppose) combobox _fayly_s_otzivami_ exists? – Steve Dec 05 '15 at 23:48
  • Steve, yes, you are right. It's a main form with fayly_s_otzivami – Богдан Лашков Dec 05 '15 at 23:50
  • 1
    From the error message this seems to be the Class name of the Form, not an instance of it – Steve Dec 05 '15 at 23:51
  • What do you mean? How can I make an instance? – Богдан Лашков Dec 05 '15 at 23:54
  • 1
    _MyFormClass frm = new MyFormClass()_ (frm is a living instance of an object whose class (type) is named MyFormClass) Only static methods could be used without using an instance of the class: _MyFormClass.AddItem_ is valid if AddItem is static otherwise you need to use the instance frm.AddItem (omitting the combo for simplicity but the concepts is the same) – Steve Dec 05 '15 at 23:59
  • But after closing the list weren't (the same code works on the form onload method. May be I should add something? – Богдан Лашков Dec 06 '15 at 00:07
  • 1
    I am sorry, but I have no idea how do you call the form that contains this code and how do you pass the instance (if you do) required to add items on the combo present in that instance. – Steve Dec 06 '15 at 00:16

0 Answers0