简体   繁体   English

从另一个 class c# 调用方法

[英]Call Methods from another class c#

I have a simple problem when calling methods in another classes.在另一个类中调用方法时,我遇到了一个简单的问题。
I have a lot of similar code in different class files and I thought that I could create a global class with methods that I use often.我在不同的 class 文件中有很多类似的代码,我认为我可以使用我经常使用的方法创建一个全局 class。
But I don't know how to do it correctly.但我不知道如何正确地做到这一点。
Please check my code below and tell me what it could be wrong.请检查我下面的代码并告诉我它可能是什么错误。
In addtion, please tell me if I could do it differently?.另外,请告诉我是否可以做不同的事情?
The Form1 contains only one button. Form1仅包含一个按钮。

namespace GlobalMethod
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void ChangeTextInButton1(string newText)
    {
        button1.Text = newText;
    }
    private void Button1_Click(object sender, EventArgs e)
    {
        GlobalMethods gm = new GlobalMethods();
        gm.ChangeTextButtonFromOtherClass();
    }
}

public class GlobalMethods
{
    public void ChangeTextButtonFromOtherClass()
    {
        Form1 f1 = new Form1();
        f1.ChangeTextInButton1("NEW BUTTON NAME");
    }
}

} }

When I call the methods works perfect and the string is passed to the ChangeTextInButton1() but the Text in button does not change and I don't know why.当我调用这些方法时效果很好, string被传递给ChangeTextInButton1()button中的Text没有改变,我不知道为什么。

instead of creating a new form, you should provide a reference to the existing one in order to modify it.您应该提供对现有表单的引用以对其进行修改,而不是创建表单。 To do this inject the dependency to the constructor of GlobalMethods :为此,将依赖项注入到GlobalMethods的构造函数中:

class GlobalMethods
{
    private readonly Form1 form;
    public GlobalMethods(Form1 f) { this.form = f; }
}

Now you can reference that form within your ChangeTextButtonFromOtherClass -method:现在您可以在ChangeTextButtonFromOtherClass方法中引用该表单:

public void ChangeTextButtonFromOtherClass()
{
    this.form.ChangeTextInButton1("NEW BUTTON NAME");
}

Finally you need to provide that reference from within your click-eventhandler:最后,您需要在 click-eventhandler 中提供该引用:

private void Button1_Click(object sender, EventArgs e)
{
    GlobalMethods gm = new GlobalMethods(this);
    gm.ChangeTextButtonFromOtherClass();
}

Alternativly to passing the reference to the constructor of GloablMethods you can also provide it to the ChangeTextButtonFromOtherClass -method itself.除了将引用传递给GloablMethods的构造函数之外,您还可以将其提供给ChangeTextButtonFromOtherClass -方法本身。

You are creating a new instance of From1 within you GlobalMethods.您正在 GlobalMethods 中创建 From1 的新实例。

So if you expect that the existing instance of Form1 from where you call globalmethods is going to change, then this is incorrect.因此,如果您期望调用 globalmethods 的 Form1 的现有实例会发生变化,那么这是不正确的。

If you want to "share" code like this then static methods is one way to do this.如果你想“共享”这样的代码,那么 static 方法是一种方法。 So you also don't need to create a new GlobalMethods instance each time, and then you just pass in the Form1 instance you want to update.所以你也不需要每次都新建一个 GlobalMethods 实例,然后传入你要更新的 Form1 实例即可。

public static class GlobalMethods
{
    public static void ChangeTextButtonFromOtherClass(Form1 f1)
    {
        f1.ChangeTextInButton1("NEW BUTTON NAME");
    }
}

(I assume that there are no other methods in this class and that you don't need to initiate an instance of it for other purposes) (我假设此 class 中没有其他方法,并且您不需要为其他目的启动它的实例)

Having said that.话说回来。 If it is only really 1 line of code, as simple as this, then although you are technically breaking the "Do not repeat yourself" rule, it is not worth having a new class to do this.如果真的只有 1 行代码,就这么简单,那么虽然你在技术上打破了“不要重复自己”的规则,但不值得拥有一个新的 class 来执行此操作。 Just repeat the same single line of code.只需重复同一行代码。

Having said that!话说回来!

If you have multiple forms which do exactly this same thing.如果你有多个 forms 做同样的事情。 Then you should instead create a new Form class which implements this functionality, and then all your forms can inherit from that new Parent Form instead.然后,您应该创建一个新的表单 class 来实现此功能,然后您的所有 forms 都可以从该新的父表单继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM