Reputation: 108
I have a method that should return the domain name of the current user in a label.text. I call the method in the load event of the form but nothing comes up, no errors in the code either. Maybe im starting the object wrong? It works if i put the method code in the load event directly.
public partial class Main Form
{
public Main()
{
InitializeComponent();
}
public string getCurrentDomain()
{
return domainNameValue.Text = Environment.UserDomainName;
}
public void Main_Load(object sender, EventArgs e)
{
Main main = new Main();
main.getCurrentDomain();
}
}
Upvotes: 1
Views: 2123
Reputation: 1827
The problem is because you are creating new instance of Main class in your Main_Load
method. So, the method getCurrentDomain()
change label text of the instance that you are creating not the label in the form where Main_Load
is executed.
Also the body of method getCurrentDomain()
violates the Principle of least astonishment because that method is generating side effect which changes text of a label. But the name of method suggest only that the current domain name is being returned.
Upvotes: 0
Reputation: 384
I think your problem is in the Main_Load
function you are creating a new form instead of changing the current form, The correct code is:
public void Main_Load(object sender, EventArgs e)
{
this.getCurrentDomain();
}
Or if you wnat to have another form just show it using main.show()
Upvotes: 6
Reputation: 5157
You could use
public string getCurrentDomain() // Method: Get current domain
{
domainNameValue.Text = Environment.UserDomainName;
return Environment.UserDomainName;
}
Upvotes: -1