peter

peter

  • NA
  • 6
  • 0

Disappearing Variable Values

Oct 21 2004 3:10 PM
I have a problem with disappearing variable values. Form1 declares string type varHost and ushort type varPort variables (initialised to "127.0.0.1" and 3333 respectively). A button btnOpenForm2 allows me to open a second Dialog type Form2 to provide an option to change the value of these variables. When Form2 is opened, 2 text boxes (txbHost and txbPort) are correctly filled with the initialised values of Form1's varHost and varPort respectively by using Form2's Activate event. Say for example I now change the value in txbPort from 5000 to 6000. When I close Form2 with the OK button. The value of varPort in Form1 has correctly changed to 6000, as demonstrated to me elsewhere in the program (please take my word for this). However, when I click the btnOpenForm2 button again, the Dialog form opens with the original value of Form1.varPort (5000) showing in Form2.txbPort rather than the new value of 6000. You might discern I'm something of a novice programmer. I certainly feel I'm missing something blatantly obvious but I'd much appreciate some plain english help on this one (hmm OK, C# and plain english in the same sentence?). By the way, the varTempPort used in Form2 is the only way I could find of avoiding a 'marshal-by-reference class' problem that occured when using the more direct :- txbPort.Text = varForm1.varPort.ToString() Form1 public class Form1 : System.Windows.Forms.Form { public string varHost = "127.0.0.1"; public ushort varPort = 5000; private System.Windows.Forms.Button btnOpenForm2; private void procOpenForm2() { Form2 varForm2 = new Form2(); varForm2.ShowDialog(); if(varForm2.DialogResult == DialogResult.OK) { varHost = varForm2.txbHost.Text; varPort = ushort.Parse(varForm2.txbPort.Text); } } private void btnOpenForm2_Click(object sender, System.EventArgs e) { procOpenForm2(); } } Form2 public class Form2 : System.Windows.Forms.Form { public System.Windows.Forms.TextBox txbHost; public System.Windows.Forms.TextBox txbPort; private void Form2_Activated(object sender, System.EventArgs e) { Form1 varForm1 = new Form1(); txbHost.Text = Form1.varHost; ushort varTempPort = Form1.varPort; txbPort.Text = varTempPort.ToString(); }

Answers (2)