public variables
-
I have a variable defined as public in form1 and am trying to assign a value to it in form2, but when I try to run it I get this error message "The name 'x' does not exist in the current context" . I have tried to search in my books and in the help files but i am just missing. I am probably over thinking it but I just can't seem to find it.
-
I have a variable defined as public in form1 and am trying to assign a value to it in form2, but when I try to run it I get this error message "The name 'x' does not exist in the current context" . I have tried to search in my books and in the help files but i am just missing. I am probably over thinking it but I just can't seem to find it.
Care to post code snippets? 1 with the variable declaration in form1 and another with the assignment statement in form2. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
I have a variable defined as public in form1 and am trying to assign a value to it in form2, but when I try to run it I get this error message "The name 'x' does not exist in the current context" . I have tried to search in my books and in the help files but i am just missing. I am probably over thinking it but I just can't seem to find it.
Declare an instance of form2, then pass in the variable value to a public method in form2 via a method in form1... --- public partial class Form1 : Form { Form2 form2; ... private void SendValueToForm2(variableValue) { form2.SetValue(variableValue) } --- HTH Jonny
-
Care to post code snippets? 1 with the variable declaration in form1 and another with the assignment statement in form2. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
form1 varible declaration public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (); public int x = 0; .... } form2 assignment statement private void Form2_Load(object sender, System.EventArgs e) { //this is where i think the problem is x = 3; }
-
form1 varible declaration public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (); public int x = 0; .... } form2 assignment statement private void Form2_Load(object sender, System.EventArgs e) { //this is where i think the problem is x = 3; }
Whoa, hold on there. That code looks so... ok, see, if you want to access a public variable of form 1 in form 2, you an object of form2 must have a reference to an object of form1. So, the code should be: public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (this); public int x = 0; .... } // Form2 class public class Form2 { private Form1 owner; // Constructor public Form2( Form1 form1) { this.owner = form1; } private void Form2_Load(object sender, System.EventArgs e) { //this is where i know the problem is owner.x = 3; } ... } with this, the Form1 object's variable x will be set inside Form2 after Form2 loads. Please read up on the concepts of classes and objects. There has to be more to life than just this
-
form1 varible declaration public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (); public int x = 0; .... } form2 assignment statement private void Form2_Load(object sender, System.EventArgs e) { //this is where i think the problem is x = 3; }
Although Praveen response is perfectly good, I consider (and he probably also does, reading his comment on the first line) that you should review your design. You are implementing one of the worst design anti-pattern, tight coupling (Re-Coupling). In this design, 2 objects depend on each other. Form1 depends on Form2 which depends on Form1. You should implement something else, maybe event-base, or interface-based. -------- "I say no to drugs, but they don't listen." - Marilyn Manson