简体   繁体   English

C# 我如何学习在另一个 Class 中调用我的方法的 Class

[英]C# How can I Learn Class that Call My Method in another Class

public class Form1:Form
{

     public Form1()
     {

     }
     Form1_Load(object Sender,EventArgs e)
     {
           SampleClass Sample=new SampleClass();
           Sample.MyMethod();
     }
}

this is first class in my project and second is这是我项目中的第一个 class ,第二个是

Hi All大家好

public class SampleClass
   {
       public void MyMethod()
       {
          //When Form1 or another class call this Method
          //I want to know it for example

         Caller.Title="Deneme";
          //
         //Unless send parametr.How Can I learn Caller class and i change it items?

       }
   }

The caller will have to pass a reference to itself into the method.调用者必须将对自身的引用传递给方法。

   public void MyMethod(Form caller)
   {
     caller.Title="Deneme";
   }

Or if you don't want the SampleClass to have a strong link to the Form class - it may be in a separate assembly that doesnt reference Windows form, you could pass in an Action that gets called with the correct string passed in.或者,如果您不希望SampleClass与表格 class 有强链接 - 它可能位于不引用 Windows 表格的单独程序集中,您可以传入一个使用传入的正确字符串调用的操作。

 Form1_Load(object Sender,EventArgs e)
 {
       SampleClass Sample=new SampleClass();
       Sample.MyMethod( title => this.Title = title );
 }

and

   public void MyMethod(Action<string> setTitle )
   {
     setTitle ("Deneme");
   }

Edit to explain delegates编辑解释代表

The Action parameter动作参数

The Action parameter on MyMethod is essentially a variable that contains code that can be run. MyMethod 上的 Action 参数本质上是一个包含可运行代码的变量。 (It does take a little head twisting to fully understand the concept.) The <string> part of the type says that we can pass in a string to this code that is going to be run. (要完全理解这个概念确实需要一点头绪。)类型的<string>部分表示我们可以将字符串传递给将要运行的代码。

Then the line that goes然后这条线

 setTitle ("Deneme");

is calling this code and passing it the text "Deneme".正在调用此代码并将文本“Deneme”传递给它。 This is the text that you want to set the Windows title to be.这是您要将 Windows 标题设置为的文本。 Now, the MyMethod method actually doesnt know that it is going to be setting the Windows title to be this text.现在, MyMethod方法实际上并不知道它将 Windows 标题设置为该文本。 This has become the responsibility of the caller.这已成为调用者的责任。 (If you want MyMethod to be absolutely certain that it is setting the Title of a Form, then the first solution is the one that you want.) (如果您希望MyMethod绝对确定它正在设置表单的标题,那么第一个解决方案就是您想要的解决方案。)

Calling打电话

The caller of the method calls方法调用的调用者

MyMethod( title => this.Title = title );

The parameter to MyMethod is: MyMethod 的参数是:

title => this.Title = title

This is the code that you are passing to the Action variable.这是您传递给 Action 变量的代码。 The title to the left of the => is the variable that will have the String that is passed to it when the code is invoked, and the stuff to the right of the => is the code that gets called. =>左侧的title是在调用代码时将具有传递给它的字符串的变量,而=>右侧的内容是被调用的代码。

So when the form calls MyMethod, it is saying I want that string and I'm going to set my title to it.因此,当表单调用 MyMethod 时,它表示我想要该字符串,并且我将为其设置标题。

The beauty of this is that MyMethod doesn't know about the form.这样做的美妙之处在于 MyMethod 不知道表单。 If you want to reuse your class when you write a Console application, you may call:如果您想在编写控制台应用程序时重用您的 class,您可以调用:

MyMethod ( title => Console.WriteLine (title) );

MyMethod doesn't need to be touched at all! MyMethod 根本不需要动!

You could try:你可以试试:

public class SampleClass
{
    public void MyMethod(Form sender)
    {
        sender.Text = "title";
    }
}

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

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