windows application
-
Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks
"Show me the way I will get there"
-
Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks
"Show me the way I will get there"
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks
"Show me the way I will get there"
You could add a constructor to the form class that accepts a parameter that is another copy of the form. Then, write a clone method that copies the data from the speficied instance into the form you're creating. IMHO, this is a silly approach to pretty much anything, but it will do what you want. If you're actually just interested in keeping just one instance of the form around and using it whenever you want, hide it when you're done with it, and show it again when you need it.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001modified on Wednesday, June 9, 2010 9:03 AM
-
Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks
"Show me the way I will get there"
Similar to John Simmons's answer, but let each form's constructor take a ClassA. Now, create the ClassA before creating either form, and pass it to the form before showing it. The important fact is that neither form 'owns' (or instantiates) the shared instance of ClassA, rather, some larger scope owns it and makes it available to both forms that want to use it. An alternative to passing the ClassA into the constructor is to make a ClassA property on each form, and assign the property before showing the form. This is a very common pattern when presenting dialogs whose purpose is to modify data that is used elsewhere. Here's a snippet:
public void DoFormStuff(ClassA ca) { using (MyForm frm = new MyForm()) { frm.ClassA = ca; frm.ShowDialog(this); } }
-
Similar to John Simmons's answer, but let each form's constructor take a ClassA. Now, create the ClassA before creating either form, and pass it to the form before showing it. The important fact is that neither form 'owns' (or instantiates) the shared instance of ClassA, rather, some larger scope owns it and makes it available to both forms that want to use it. An alternative to passing the ClassA into the constructor is to make a ClassA property on each form, and assign the property before showing the form. This is a very common pattern when presenting dialogs whose purpose is to modify data that is used elsewhere. Here's a snippet:
public void DoFormStuff(ClassA ca) { using (MyForm frm = new MyForm()) { frm.ClassA = ca; frm.ShowDialog(this); } }