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();
}
}
}