Why property or field unable to give the contents ?
-
i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:
-
i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:
instead of initializing st in Form_Load ,initialize it in the constructor.
"Change is the only constant thing in life Either or survive or get extinct"
-
i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:
If you want to access a value that is present in the current instance of the form, you need a reference to that instance. If you create a new instance of the form, you will always get the same value, as it's a newly created and unused form.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
-
instead of initializing st in Form_Load ,initialize it in the constructor.
"Change is the only constant thing in life Either or survive or get extinct"
-
i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:
kalaveer wrote:
private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; }
kalaveer wrote:
Form1 f = new Form1(); MessageBox.Show(f.s);
You have initialised your variable when the Form Loads (Form_LOad event). This event is fired when the form is loaded (ie. displayed). As you have not shown your form your property has not been set
Form1 f = new Form1(); f.Show(); // important! MessageBox.Show(f.s);
-
i have declared a public propery like below in form1 public partial class Form1 : Form { string st; public string s { get { return st; } } private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; } } and i tried to display this value in form2 using the statements Form1 f = new Form1(); MessageBox.Show(f.s); its not giving any errors but displaying nothing in messagebox but if i initialize the st at the declatation instead of form1_load its displaying correct value ex: public string st="MT PROPERTY"; please tell me why thats happening and how can i resolve this.:confused:
In your example, the Form_Load event has not been fired. You need to set the value in the constructor based on your logic.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before. -
kalaveer wrote:
private void Form1_Load(object sender, EventArgs e) { st = "MY PROPERTY"; }
kalaveer wrote:
Form1 f = new Form1(); MessageBox.Show(f.s);
You have initialised your variable when the Form Loads (Form_LOad event). This event is fired when the form is loaded (ie. displayed). As you have not shown your form your property has not been set
Form1 f = new Form1(); f.Show(); // important! MessageBox.Show(f.s);
Wow thats working but it displaying the form again how can i avoid this. my first form already visible and with f.show() its displayed again i tried to make invisible but by setting visible property to false not working as f.show is just opposite of hiding na please suggest:|
-
Wow thats working but it displaying the form again how can i avoid this. my first form already visible and with f.show() its displayed again i tried to make invisible but by setting visible property to false not working as f.show is just opposite of hiding na please suggest:|
kalaveer wrote:
Wow thats working but it displaying the form again how can i avoid this.
As I already explained, you need a reference to the existing form, not creating a new one.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.