Answered by:
Methods from another form

Question
-
Hello, I have 2 forms in my application (form1-the main, form2). How can i use in form2 methods that i wrote in form1?Saturday, May 16, 2009 12:07 PM
Answers
-
yes make the Form2 methods public and thenForm2 _FrmObj = new Form2();_FrmObj.MethodName();If your Form2 methods are static then you can directly call them as Form2.MethodName()
Ganesh Ranganathan
[Please mark the post as answer if you find it helpful]Saturday, May 16, 2009 12:24 PM -
That depends on your Form2 methods that you want to call from Form1Suppose Form2 has a static method DoWork(), then you will call it from Form1 as Form2.DoWork();If the DoWork method is nonstatic, then you would call it asForm2 _frmObj = new Form2();_frmObj.DoWork();
Ganesh Ranganathan
[Please mark the post as answer if you find it helpful]Saturday, May 16, 2009 2:52 PM -
You can make a class method static if it doesn't reference any non-static class members. That isn't often the case in a Form, you'd typically at least reference its controls. Easy enough to find out, put "static" in front of the method header, the compiler will complain loudly if it can't be static.
To call a non-static method, you need a reference to the form object. Note that Ganesh's snippet is rarely good enough to get one, his _frmObj would reference to a new instance of Form2 that is not visible.
Hans Passant.Saturday, May 16, 2009 9:55 PMModerator -
Red flag.
If you notice that two or more separate classes need to use identical methods, and those classes are unrelated to one another...Such as, neither class inherits the other, neither class shares a common base class that makes the methods available....A big red flag should go up.
That flag should tell you that you need to put that method into a separate helper class and allow your original two class to access the method in that fashion. This can be done with a static class or static methods defined within a class. Or this can be done by creating instance objects of this helper class.
You could even pass the same instance of this helper object to both of your classes, so you could do some neat OOP stuff.
Hope this helps.
Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."Monday, May 18, 2009 4:48 PM -
Normally, you would create events on sibling forms that the MainForm would subscribe to. However, in some cases, you can pass events over, though I do not see that as necessarily good practice.
See:
Passing Values Values Between Forms
Also see:
Passing Methods in .NET 3.5
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.comMonday, May 18, 2009 6:16 PM
All replies
-
yes make the Form2 methods public and thenForm2 _FrmObj = new Form2();_FrmObj.MethodName();If your Form2 methods are static then you can directly call them as Form2.MethodName()
Ganesh Ranganathan
[Please mark the post as answer if you find it helpful]Saturday, May 16, 2009 12:24 PM -
-
That depends on your Form2 methods that you want to call from Form1Suppose Form2 has a static method DoWork(), then you will call it from Form1 as Form2.DoWork();If the DoWork method is nonstatic, then you would call it asForm2 _frmObj = new Form2();_frmObj.DoWork();
Ganesh Ranganathan
[Please mark the post as answer if you find it helpful]Saturday, May 16, 2009 2:52 PM -
You can make a class method static if it doesn't reference any non-static class members. That isn't often the case in a Form, you'd typically at least reference its controls. Easy enough to find out, put "static" in front of the method header, the compiler will complain loudly if it can't be static.
To call a non-static method, you need a reference to the form object. Note that Ganesh's snippet is rarely good enough to get one, his _frmObj would reference to a new instance of Form2 that is not visible.
Hans Passant.Saturday, May 16, 2009 9:55 PMModerator -
Red flag.
If you notice that two or more separate classes need to use identical methods, and those classes are unrelated to one another...Such as, neither class inherits the other, neither class shares a common base class that makes the methods available....A big red flag should go up.
That flag should tell you that you need to put that method into a separate helper class and allow your original two class to access the method in that fashion. This can be done with a static class or static methods defined within a class. Or this can be done by creating instance objects of this helper class.
You could even pass the same instance of this helper object to both of your classes, so you could do some neat OOP stuff.
Hope this helps.
Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."Monday, May 18, 2009 4:48 PM -
Normally, you would create events on sibling forms that the MainForm would subscribe to. However, in some cases, you can pass events over, though I do not see that as necessarily good practice.
See:
Passing Values Values Between Forms
Also see:
Passing Methods in .NET 3.5
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.comMonday, May 18, 2009 6:16 PM -
Yup, events are another way.
For me personally, +90% of the time when I run into this. The method really is in the wrong class.
Mark the best replies as answers. "Fooling computers since 1971."Monday, May 18, 2009 6:19 PM