I have 2 .cs files each with a class in it. How do I call a method in a class from Form1.cs in another class inside of Form2.cs?
Looks something like this...
Form1.cs
public partial class Class1 : ClassContainer
{
public void awesomeMethod()
{
}
}
Form2.cs
class Class2 : SomethingChanged
{
public void decentMethod()
{
}
}
I would like to call awesomeMethod() inside of the decentMethod(). Thanks.
6 Answers
You mean, like this?
public void decentMethod()
{
Class1 instance = new Class1();
instance.awesomeMethod();
}
You need an instance of the class you want to call the method on.
Or, if you don't need/want to work with an instance, make it the method static:
public partial class Class1 : ClassContainer
{
public static void awesomeMethod()
{
}
}
...
public void decentMethod()
{
Class1.awesomeMethod();
}
In order to call an instance method of a class, you need an instance of a class. Thus, to call Class1's awesomeMethod, you must create an instance of Class1:
Class1 c = new Class1();
c.awesomeMethod();
From your opening paragraph, though, it sounds like the two actual classes are two different forms. In that case, it doesn't really make sense for one form to create a new instance of another form purely to get at what is presumably a helper method - creating that second form is potentially an awful lot of overhead. You'd be better off putting helper methods into a separate, more lightweight class.
I'm guessing that these aren't real method signatures.
Does awesomeMethod need any references to any of the members of decentMethod?
If not, it would be as simple as:
new Class1().awesomeMethod();
Ok i am a beginner at this and wondered the same thing. I believe I understand what you're asking. I just tried this and after a few attempts it clicked, and being newer to this maybe I can simplify it with my words. I don't know which program you are using but I used Visual Studio 2016. I created two forms(Form1 and Form2) each containing a button. Form1 button and Form2 button. When I click Form1 button it calls a method from form2, and when I click Form2 button it calls a method from Form1. I noticed that in the solution explorer it shows both forms, and everything they contain. I've done a lot of research and correct me if I am wrong but I believe the order goes like this from parent container to child( Namespace>Class>Method ). Believing this to be true I figured that I would need to call the class before I could call the method.
These are my scripts.
Form1 :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
private void f1button_Click(object sender, EventArgs e)
{
f2Words f2w = new f2Words();
f2w.Words2();
}
}
public class f1Words
{
public void Words1()
{
MessageBox.Show("Form 1 Method Calling Worked!");
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void f2button_Click(object sender, EventArgs e)
{
f1Words f1w = new f1Words();
f1w.Words1();
}
}
public class f2Words
{
public void Words2()
{
MessageBox.Show("Form 2 Method Calling Worked!");
}
}
In the Solution Explorer window it shows the classes of both Form1 and Form2. So I decided to try to call new instances of these classes.
"f1Words f1w = new f1words()" and "f2Words f2w = new f2Words()".
Then right after this I called those new instances of the classes into play with the methods they contained.
"f1w.Words1()" and "f2w.Words2()"
The end result was a success. When I click Form1's Button1 it called Form2's F2Words Class and pulled the Words2 Method out of it opening a message box saying "Form2 Method Calling Worked!", and visa versa for the Form2 Button2.
As this post is 4 years old I assume you've already found this out on your own and perhaps found a better solution, but for anyone else asking this same question in the future I hope this helps you out.
Here is something you can try in Visual Studio. Write your use of the method without the definition. For example, somewhere in your code, type:
cls_a instance_a;
Then you get a squiggly line under cls_a. Right click "cls_a" and select generate from the popup menu. Then see what happens.
I saw App_Code directory created, and the class cls_a was generated in a file cls_a.cs in that directory. Visual Studio generated the definition. And it compiles.
You can then type the use of a method without the definition from cls_a like this:
instance_a.meth_x();
Again, right-click on the squiggly line and select generate. Let Visual Studio generate the definition for you.
I think ASP .NET requires you to put your additional classes in the App_Code subdirectory. And if you need to bend ASP.NET to your way, I think the Web.config file can be edited so you can put your code files wherever you want. I have not reviewed how to do this so I leave that to you or someone else.
Anyway, not having your classes and methods in App_Code subdirectory can cause your classes and functions to not be seen in your web form code, even though everything else is correct.
Assume the main Form is frmMain. Make a static declaration outside frmMain()'s constructor; assign it inside that same constructor:
public static frmMain p_frmMain = null;
public frmMain()
{
InitializeComponent();
:
p_frmMain = this;
}
From another class (using main's namespace in that same solution), call, say, main's 'btnHelloWorld_Click()' method:
AnotherClass_EventHandler(object sender, EventArgs e)
{
: // Call frmmain()'s 'btnHelloWorld_Click' event:
frmMain.p_frmMain.btnHelloWorld_Click (sender, e);
:
}
// Quick, dirty and done.