C# 在另一个 .cs 文件类中使用一个 .cs 文件类中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11778642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a method from one .cs file class in another .cs files class
提问by Garrett
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?
我有 2 个 .cs 文件,每个文件都有一个类。如何在 Form2.cs 内的另一个类中从 Form1.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.
我想在descendMethod() 内部调用awesomeMethod()。谢谢。
回答by sloth
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();
}
回答by paul
I'm guessing that these aren't real method signatures.
我猜这些不是真正的方法签名。
Does awesomeMethodneed any references to any of the members of decentMethod?
是否awesomeMethod需要引用 的任何成员decentMethod?
If not, it would be as simple as:
如果没有,那就很简单了:
new Class1().awesomeMethod();
回答by Chris
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 的 awesomeMethod,您必须创建 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.
但是,从您的开头段落中,听起来这两个实际类是两种不同的形式。在这种情况下,一个表单创建另一个表单的新实例纯粹是为了获得可能是一个辅助方法的东西并没有真正意义 - 创建第二个表单可能会产生大量开销。您最好将辅助方法放入一个单独的、更轻量级的类中。
回答by Indinfer
Here is something you can try in Visual Studio. Write your useof the method withoutthe definition. For example, somewhere in your code, type:
以下是您可以在 Visual Studio 中尝试的内容。在没有定义的情况下写出您对方法的使用。例如,在您的代码中的某处键入:
cls_a instance_a;
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.
然后你会在 cls_a 下看到一条波浪线。右键单击“cls_a”并从弹出菜单中选择生成。然后看看会发生什么。
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.
我看到创建了 App_Code 目录,并且在该目录中的文件 cls_a.cs 中生成了类 cls_a。Visual Studio 生成了定义。它编译。
You can then type the use of a method without the definition from cls_a like this:
然后,您可以键入不带 cls_a 定义的方法的使用,如下所示:
instance_a.meth_x();
instance_a.meth_x();
Again, right-click on the squiggly line and select generate. Let Visual Studio generate the definition for you.
再次,右键单击波浪线并选择生成。让 Visual Studio 为您生成定义。
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.
我认为 ASP .NET 要求您将其他类放在 App_Code 子目录中。如果您需要按照自己的方式调整 ASP.NET,我认为可以编辑 Web.config 文件,这样您就可以将代码文件放在任何您想要的位置。我还没有回顾如何做到这一点,所以我把它留给你或其他人。
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.
无论如何,在 App_Code 子目录中没有您的类和方法会导致您的类和函数在您的 Web 表单代码中看不到,即使其他一切都是正确的。
回答by csharpnub
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.
好吧,我是这方面的初学者,想知道同样的事情。我相信我明白你在问什么。我刚刚尝试过这个,经过几次尝试后,它点击了,并且对这个比较新,也许我可以用我的话来简化它。我不知道您使用的是哪个程序,但我使用了 Visual Studio 2016。我创建了两个表单(Form1 和 Form2),每个表单都包含一个按钮。Form1 按钮和 Form2 按钮。当我单击 Form1 按钮时,它会从 form2 调用一个方法,当我单击 Form2 按钮时,它会从 Form1 调用一个方法。我注意到在解决方案资源管理器中它显示了两种形式,以及它们包含的所有内容。我做了很多研究,如果我错了,请纠正我,但我相信从父容器到子容器的顺序是这样的( Namespace>Class>Method )。
These are my scripts.
这些是我的脚本。
Form1 :
表格1:
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:
表格2:
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.
在解决方案资源管理器窗口中,它显示了 Form1 和 Form2 的类。所以我决定尝试调用这些类的新实例。
"f1Words f1w = new f1words()" and "f2Words f2w = new f2Words()".
“f1Words f1w = new f1words()”和“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()"
“f1w.Words1()”和“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.
最终结果是成功的。当我单击 Form1 的 Button1 时,它调用 Form2 的 F2Words 类并从中拉出 Words2 方法,打开一个消息框,上面写着“Form2 方法调用有效!”,对于 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.
由于这篇文章已有 4 年的历史,我假设您已经自己发现了这一点,并且可能找到了更好的解决方案,但是对于将来提出同样问题的其他人,我希望这对您有所帮助。
回答by Xeno
Assume the main Form is frmMain. Make a static declaration outside frmMain()'s constructor; assign it inside that same constructor:
假设主窗体是 frmMain。在 frmMain() 的构造函数之外做一个静态声明;在同一个构造函数中分配它:
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:
从另一个类(在同一个解决方案中使用 main 的命名空间),调用 main 的 'btnHelloWorld_Click()' 方法:
AnotherClass_EventHandler(object sender, EventArgs e)
{
: // Call frmmain()'s 'btnHelloWorld_Click' event:
frmMain.p_frmMain.btnHelloWorld_Click (sender, e);
:
}
// Quick, dirty and done.

