c# - Access form label from another class/namespace -


i know has been asked thousands of time still after lot of research can't find solution , sorry post.

i want access label class in namespace. sample of code understand better trying do:

public partial class main : form     {         public main()         {             initializecomponent();         }     }    //class in namespace class servers     {         public void _setlabeltext()         {            main.label1.text = "new text";         }     } 

how supposed proper way?

one option store reference form in constructor this:

public class servers {     private form _frmmain;     public servers(form frmmain)     {         _frmmain = frmmain;     }     public void setlabeltext()     {         _frmmain.label1.text = "new text";     } } 

and use this:

public partial class main : form {     public main()     {         initializecomponent();         var servers = new servers(this);         servers.setlabeltext();     } } 

however, it's typically advised return form class , set there, this:

public partial class main : form {     public main()     {         initializecomponent();         label1.text = servers.gettextforlabel();     } } public class servers {     public static string gettextforlabel()     {        return "new text"; //(i assume more complex)     } } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -