0

I have created an helper function inside my Windows Form source code (is that the right place?), how can I reference variables created elsewhere in the same form source code?

In the following example myVar is declared in myForm and myTextBox is drawn in the designer. Both give as error: "An object reference is required for the non-static field, method, or property".

I know that if I define myVar as static the code would work, but what if I then decide to change its value? As for the text box, changing it to static in the designer file doesn't correct the error..

(I took a look at other questions, but the solution proposed there don't seem to fit in with my code).

myForm.cs

namespace myThing
{
    public partial class myForm : Form
    {
        public int myVar;

        public myForm()
        {
            InitializeComponent();

            myVar = 0;

        }

        static void foo(Point checkPoint, List<Vertex> checkList)
        {
            int myNumber = myForm.myVar;

            myForm.myTextBox.Text = "";
            //OR
            System.Windows.Forms.TextBox.myTextBox.Text = "";
        }

my.Form.Designer.cs

namespace myThing
{
    partial class myForm
    {
        //a bunch of stuff goes here

        private System.Windows.Forms.PictureBox myPictureBox;
        private System.Windows.Forms.TextBox myTextBox;
    }
}
5
  • 1
    Did you try public instead of static? Commented Apr 2, 2016 at 10:07
  • tried for both now, it doesn't work. Commented Apr 2, 2016 at 10:20
  • 1
    Your helper method/function is declared static. A static method cannot access instance variables as myVar. Remove the static keyword from foo and it should work. Commented Apr 2, 2016 at 10:27
  • that doesn't seem to work either.. Commented Apr 2, 2016 at 10:48
  • As far as myVar is concerned I had to set both it and foo to public, then it worked. Commented Apr 2, 2016 at 14:22

2 Answers 2

1

You cannot refernce an instance variable in a static method without passing in the instance of the class the variable is defined on.

Either make your variable declaration static (probably not what you want to do) or make the foo() function non-static, or pass into the foo() method an instance of the form on which the variable exists.

You seem to be making a conceptual error - a Form is not a visual structure as such - its just a class declaration. You can have as many instances of the same Form existing in memory at any one time as you like. It behaves exactly like an other class declaration. If you are coming from MS Access or similar this might seem weird.

For instance myForm variable inside static Foo() has no value - it does not indicate any specific instance of the class myForm so wont compile unless its being used exclusively to reference Static properties of the class.

Sign up to request clarification or add additional context in comments.

3 Comments

wouldn't myForm be the same as "this"?
do you mean pass an instance of the form in foo as in passing a parameter? I guess not (it doesn't work).
No - myForm is a class name. "this" is a pointer to an instance of class whose Type is myForm.
0

It seems the solution to this problem was to set the foo method to public.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.