Global Variables ...wtf
-
Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)
-
Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)
-
Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)
You may need an independant storage mechanism. Essentially a container for global data. Alternatively, the native API call supports passing user-defined data to the window create command, I believe. Besides, there isn't anything wrong with global data--it just needs to be managed. Finally, I'm not exactly too sure what the problem is--can't you just pass the data as part of the constructor call to the other form? Marc STL, a liability factory - Anonymously
A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
Latest AAL Article My blog -
Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)
Orlanda Ramos wrote: Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? As pssing properties in the constructor could be perceived as creating a dependency, the other way to do this would be to provide public or internal accessors for the properties you want to pass in form 2.
public class Form2 : Form { private int myInt; private string myString; public int MyInt { set { myInt = value; } } public string MyString { set { myString = value; } } public Form2() { } }
public class Form1 : Form { public Form1() { } private void OpenForm2(int myInt, string myString) { Form2 form2 = new Form2(); form2.MyInt = myInt; form2.MyString = myString; form2.Show(); } }
-
Orlanda Ramos wrote: Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? As pssing properties in the constructor could be perceived as creating a dependency, the other way to do this would be to provide public or internal accessors for the properties you want to pass in form 2.
public class Form2 : Form { private int myInt; private string myString; public int MyInt { set { myInt = value; } } public string MyString { set { myString = value; } } public Form2() { } }
public class Form1 : Form { public Form1() { } private void OpenForm2(int myInt, string myString) { Form2 form2 = new Form2(); form2.MyInt = myInt; form2.MyString = myString; form2.Show(); } }
This is the way I would do it, just because it is more flexible. If you use either method I would say that you aren't creating a dependency between the two forms, but a dependency on the data required for Form2 to work correctly. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him
-
Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)
You can build a class which is called GlobalVars NameSpace GlobalVars { public class GlobalVars { public static string stringVars; public class GlobalVars() {} } }
-
You can build a class which is called GlobalVars NameSpace GlobalVars { public class GlobalVars { public static string stringVars; public class GlobalVars() {} } }
whoops, in this example the constructor will never be called automatically - to make this a fully static class the constructor line should be:
static GlobalVars()
notpublic class GlobalVars()
The advantage of using a static constructor is that it allows you to dynamically set values/execute code before any properties are sent to the requesting class. Note that you cannot control exactly when a static constructor is initiated, but you can rest assured that it will happen before any values are passed in or out of your static class. -
Orlanda Ramos wrote: Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? As pssing properties in the constructor could be perceived as creating a dependency, the other way to do this would be to provide public or internal accessors for the properties you want to pass in form 2.
public class Form2 : Form { private int myInt; private string myString; public int MyInt { set { myInt = value; } } public string MyString { set { myString = value; } } public Form2() { } }
public class Form1 : Form { public Form1() { } private void OpenForm2(int myInt, string myString) { Form2 form2 = new Form2(); form2.MyInt = myInt; form2.MyString = myString; form2.Show(); } }
That wraps it up preaty niely ;-) Thanks everyone, Furty! Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)