0

I'm trying to access a function in the form of my Windows Forms app from a child class file, and it gives the error

'An object reference is required for the non-static method, field, or property 'Form1.UpdateCube()''

I am calling this from the class Cube.cs, and the call looks like this:

Form1.UpdateCube();

And this is the UpdateCube function in Form1.cs:

public void UpdateCube()
{
    if (BTL.BackColor != mapColor(c1.B.TR))
    {
        BTL.BackColor = mapColor(c1.B.TR);
        System.Console.Write("Mapping");
    }
    //And so on
}

So why can't I access the function in Form1?

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
MattyAB
  • 365
  • 2
  • 9
  • 19
  • 2
    mark `UpdateCube` as `static` – Adnan Umer Mar 29 '16 at 20:51
  • @AdnanUmer Then it won't be able to access static members, and will fail to compile for that reason. – Servy Mar 29 '16 at 20:58
  • If you haven't instantiated the class (`var formRef = new Form1()`) or the reference has gone out of scope then you'll get this error. Though if you're displaying the form you should have this somewhere already. – ChrisF Mar 29 '16 at 20:59
  • What does the calling class look like? How is it created? The easiest thing to do would be to pass the instance of `Form1` that creates the class to the child class as a constructor parameter – Jason Watkins Mar 29 '16 at 21:01
  • Your `Cube` class almost certainly shouldn't be trying to access the form in the first place. You should have the UI and non-UI kept separate. If you want to have a means for a business class to indicate that there is progress on some long running operation to the UI, then use the `Progress` class; it's specifically designed to do just that. – Servy Mar 29 '16 at 21:02
  • @JasonWatkins But that would be very poor design, as you're now tightly coupling that logic with the UI. – Servy Mar 29 '16 at 21:02
  • Probably so, but without far more information it's impossible to judge that accurately. Regardless, it's almost certainly the only technically feasible answer to the question asked. – Jason Watkins Mar 29 '16 at 21:06
  • So I don't want the UpdateCube function to be static, as it is a function specific to updating the current instance of the form. I want the cube class to have all the stuff dealing with the logic of the cube, and the Form class dealing with the backend of the UI (Event handlers, object color updaters etc.) – MattyAB Mar 30 '16 at 20:57

1 Answers1

0

As the error message tells you, you can only call instance methods on an instance of a class, not on the type itself. Therefore, you need an instance of Form1 to call UpdateCube on.

The easiest solution is probably to pass the current instance in when creating the child class like this:

public class SomeClass
{
    private readonly Form1 parent;

    public SomeClass(Form1 form)
    {
        this.parent = form;
    }

    public void DoStuff()
    {
        this.parent.UpdateCube()
    }
}

public partial class Form1
{
    private SomeClass CreateChild()
    {
        return new SomeClass(this);
    }

    public void UpdateCube()
    {
        if (BTL.BackColor != mapColor(c1.B.TR))
        {
            BTL.BackColor = mapColor(c1.B.TR);
            System.Console.Write("Mapping");
        }
        //And so on
    }

    // The rest of the class
}
Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
  • Isn't this initializing the Form from the Cube.cs? This is not what I want, as the code is all written to have the cube.cs as a child of the Form1.cs (hence the title of the question) – MattyAB Mar 30 '16 at 21:02
  • No, you are not initializing the form in Cube.cs. You are only storing a reference to an existing instance of `Form1` in Cube.cs so that you can use it to call `UpdateCube` – Jason Watkins Mar 30 '16 at 21:06
  • This sounds like the right sort of thing... It compiled ok, but when I ran it it crashed VS D: – MattyAB Mar 30 '16 at 21:28
  • I'm guessing I had to remove the point where I was previously initing the Cube class, which looked like this: c1 = new Cube(); – MattyAB Mar 30 '16 at 21:29
  • So the second time I ran the program, it threw some exception saying An unhandled exception of type 'System.NullReferenceException' occurred in CubeSolver.exe – MattyAB Mar 30 '16 at 21:31
  • See [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jason Watkins Mar 30 '16 at 21:32