0

I am new in C# programming, but I know that attributes should be encapsulated in the class. Therefore I used private declaration

private string _servername;

Problem is that I cannot update _servername from 2nd Form. I clicked on ChangeName button. New Form2 was appeared. I clicked on button NewName. Methods from Form1 were finished successfully and MessageBox in method _Server2 shown me that value of _servername should be "TEST" When I click on button CurrentServerName, value of _servername is still "localhost". Could you please tell me why? How to fix it? Thank you in advance

FORM 1

namespace Test
{
    public partial class Form1 : Form
    {
        private string _servername;
        public Form1()
        {
            InitializeComponent();
            _servername = "localhost";
        }

        public string _Server(string _server)
        {
            _servername = _server;
            string serverlist2 = _server;            
            _Server2(serverlist2);
            return serverlist2;
        }

        public void _Server2(string _server2)
        {
            _servername = _server2;
            MessageBox.Show(_servername);
        }

        private void ChangeName_Click(object sender, EventArgs e)
        {
            Form2 _Form2 = new Form2();
            _Form2.Show();
        }

        private void CurrentServerName_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_servername);
        }

    }
}

FORM 2

namespace Test
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void NewName_Click(object sender, EventArgs e)
        {
            Form1 _Form1 = new Form1();
            _Form1._Server("TEST");
            this.Close();
        }
    }
}
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
cotablise
  • 65
  • 6
  • By 'attributes should be encapsulated in a class' I think you mean 'members should be encapsulated in a class'. Attributes are another thing all together http://www.devx.com/dotnet/Article/11579/1954 – Darbio Feb 08 '12 at 12:32
  • No dramas, glad to have been of help! – Darbio Feb 08 '12 at 23:12

2 Answers2

2

This doesn't work because you are creating a new "instance" of Form1 inside Form2, this is not the same as the other Form1.

You need to change Form1 like this:

private void ChangeName_Click(object sender, EventArgs e) 
{ 
    using (Form2 form2 = new Form2())
    {
        form2.Show();
        _Server(form2.ServerName);
    } //Ensure the form is Disposed.
} 

And Form2 like this:

namespace Test   
{   
    public partial class Form2 : Form   
    {   
        public Form2()   
        {   
            InitializeComponent();   
        }   

        public String ServerName { get; private set; } //Can only be set in this class, but read by all.

        private void NewName_Click(object sender, EventArgs e)   
        {   
            ServerName = "Test";   
            Close();   
        }   
    }   
}  

EDIT: Another point. Naming methods like _Server() and local variables like _Form2 is bad practice, they shold be Server() and form2. Have a look at this MSDN article and also this question.

Community
  • 1
  • 1
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
0

You don't want to create a new instance of Form1 inside Form2. Instead, when you create an instance of Form2 to show it, pass a reference to the current instance of Form1 into the constructor. You can then access the methods on the existing instance of Form1. Here is an example:

Form 1

private void ChangeName_Click(object sender, EventArgs e)
{
    // Pass a reference to this form instance using the 'this' keyword
    Form2 _Form2 = new Form2(this);
    _Form2.Show();
}

Form 2

namespace Test
{
    public partial class Form2 : Form
    {
        // Hold an instance to Form1
        private Form1 mForm1;

        public Form2(Form1 form1)
        {
            // Store instance to form1
            mForm1 = form1;

            InitializeComponent();
        }

        private void NewName_Click(object sender, EventArgs e)
        {
            // Change name on existing instance of form 1
            mForm1._Server("TEST");
            this.Close();
        }
    }
}
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55