I have two forms that share data between eachother.

  1. In Designer view of Form2 I establish a public variable.

    public bool isMyFormValid

  2. When I attempt to access the variable in Form1 like this...

    if (Form2.isMyFormValid = true) { //do this}

C Sharp no likey... I get this message: Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form2.isMyFormValid' C:\Users\tthompso\documents\visual studio 2010\Projects\practice\practice\Form1.cs 89 23 practice

Clearly there is a basic principal I'm not understanding. Can someone point me to a referece that explains this concept or provide a helpful example?

Recommended Answers

All 3 Replies

Your variable 'isMyFormValid' is an instance variable, meaning that you need to create a Form2 object and access it through that instance.

Alternatively, you may declare static the variable isMyFormValid.

Momerath,
That was the approach I used to resolve my problem. In Form1 I initially called the Form2 by creating an object of the Form...

Form2 newForm2 = new form2();

So my problem was in Form1 I was attempting to access the variable using the Form name followed by a period than the variable.

Form2.boolVariable  //<--- Not a chance!

Using your suggestion I took the object I created of Form2 and accessed the varable using that instead. So far it looks like a solution to me. thanks

newForm2.boolBariable //<--- works fine
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.