我如何...假设您的项目中有两个类。您的项目中每个都有一个方法。如何在一个事件中在项目中使用它们 [英] How do I...Suppose there is two class in your project. There are one one methods each in your project. How to consume them in your project in one event

查看:76
本文介绍了我如何...假设您的项目中有两个类。您的项目中每个都有一个方法。如何在一个事件中在项目中使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



任何人都可以回答这个问题。



谢谢

解决方案

请告诉我

i在我的项目中有两个课程,每个课程包含一个方法。

如何在一个事件中调用这两个方法。

...

实例方法。




实例方法需要各自类的实例 - 因此,为了从同一事件处理程序中的不同类调用两个实例方法,您需要找到一种方法将实例放入方法中。



如果实例包含在类(例如表单)中并存储为变量,那么这很容易:

 < span class =code-keyword> public   class  MyForm:Form 
{
private MyClass1 mc1 = new MyClass1();
private MyClass2 mc2 = new MyClass2();
...
void MyForm_Event( object sender,EventArgs e)
{
mc1.MyMethod1();
mc2.MyMethod2();
}
}

如果不是,那么它会变得更加复杂,因为你必须找到一种方法来传递实例(或者在事件处理程序中创建它们,但是这意味着你得到新的实例,它不会影响方法之外的任何实例)


我认为解决方案是多播代表:



委托对象的一个​​有用属性是可以使用+运算符将多个对象分配给一个委托实例。多播委托包含已分配的委托的列表。调用多播委托时,它将按顺序调用列表中的委托。只能组合相同类型的代表。



请检查以下内容:





https://msdn.microsoft.com/en-us/library/ms173175.aspx [ ^ ]

Hi all

Can anyone please answer this question.

Thank you

解决方案

"please tell me
i have 2 classes in my project, which contains one method each.
how to call both of the methods in a single event.
...
instance methods."


Instance methods require instances of their respective classes - so in order to call two instance methods from different classes in the same event handler, you need to find a way to get the instances into the method.

If the instances are contained within a class (such as a form) and stored as variables, then that's easy:

public class MyForm : Form
   {
   private MyClass1 mc1 = new MyClass1();
   private MyClass2 mc2 = new MyClass2();
   ...
   void MyForm_Event(object sender, EventArgs e)
      {
      mc1.MyMethod1();
      mc2.MyMethod2();
      }
   }

If they aren't, then it gets more complex as you have to find a way to pass the instances through (or create them in the event handler, but that means you get new instances, and it won't affect any instances outside the method)


I think the solution is Multicast Delegates :

A useful property of delegate objects is that multiple objects can be assigned to one delegate instance by using the + operator. The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined.

Please check the following :


https://msdn.microsoft.com/en-us/library/ms173175.aspx[^]


这篇关于我如何...假设您的项目中有两个类。您的项目中每个都有一个方法。如何在一个事件中在项目中使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆