学习代表和活动 [英] Learning delegates and events

查看:77
本文介绍了学习代表和活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是C#的新手,想要在控制台应用程序中创建自己的活动。

但是我在精神上苦苦挣扎,并查看了MSDN

文档,但我得知空白。网上有什么地方

显示一个非常基本的代表和一个非常基本的事件?一些不错的

简单的示例代码就是我正在寻找的,没有任何毛病。


谢谢!


-

Mark Allison,SQL Server MVP
http://www.markallison.co.uk

寻找SQL Server复制书?
http://www.nwsu.com/0974973602m.html

解决方案

我今天肯定会在这两篇文章上发一篇简短的文章,但是在

的同时,我很确定你应该觉得这很有帮助。


委托是一种能够封装方法的类型

签名。代表可以与事件一起使用。活动

是一种通常在班级内宣布的类型。

公共班MyClass

{

/ /指向要调用的方法的委托。在这种情况下,

//方法的返回类型是''void''。

public delegate void MyDelegate();

//如果要处理事件的方法

//(本例中为MyMethod)接受参数,则声明

//委托如下

public delegate void MyDelegate(int i);

//事件类型为MyDelegate

公共事件MyDelegate MyEvent;

public MyClass()

{

//将事件附加给代表

this.MyEvent + = new MyDelegate(MyMethod);

}

//这是充当事件过程的方法。

public void MyMethod()

{

//在此处理活动。

}

}


问候,

JVRavichandran

- http://www.geocities .com /

jvravichandran

- http://www.411asp.net/func/search?

qry = Ravichandran + JV& cob = aspnetpro

- http://www.southasianoutlook.com

- < a rel =nofollowhref =http://www.MSDNAA.Nettarget =_ blank> http://www.MSDNAA.Net

- http://www.csharphelp.com

- http://www.poetry.com/Publications/

显示。 asp?ID = P3966388& BN = 999& PN = 2

- 或者,只需在
http://www.Google.comtarget =_ blank> http://www.Google.com



***通过Developersdex发送 http://www.developersdex.com ***

Don'不要只参加USENET了为它辩护!


谢谢,


好​​的,让我用自己的话说,然后你可以打我下。 ;-)


您创建一个名为public delegate的委托void MyDelegate(int i) -

此委托使用相同的sig运行代码。很酷,明白了。


然后您按如下方式定义您的活动:

公共活动MyDelegate MyEvent;


我并没有真正理解上面的代码行。那是什么

有效宣告?


当你把事件附加给代表时,你是解雇事件,

举起活动?我希望从另一个班级收听该活动

。我如何让其他班级听这堂课的
活动?


对不起但是你这个非常简单的例子让我更加困惑。

-

Mark Allison,SQL Server MVP
http://www.markallison.co.uk

寻找SQL Server复制书?
http://www.nwsu.com/0974973602m.html

Ravichandran JV写道:

我今天肯定会在这两篇文章上发一篇简短的文章,但同时,我很确定你应该觉得这很有帮助。
委托是一种能够封装方法签名的类型。代表可以与事件一起使用。事件
是一种通常在类中声明的类型。

公共类MyClass
{
//指向要调用的方法的委托。在这种情况下,
//方法的返回类型是''void''。
public delegate void MyDelegate();
//如果要处理事件的方法
//(本例中为MyMethod)接受参数,声明
//委托如下
public delegate void MyDelegate(int i);
//事件类型MyDelegate
公共事件MyDelegate MyEvent;
public MyClass()
//将事件附加到代表
this.MyEvent + = new MyDelegate(MyMethod);
}
//这是充当事件过程的方法。
public void MyMethod()
//
//在此处理事件。
}
}


JVRavichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry = Ravichandran + JV& cob = aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID = P3966388& BN = 999& PN = 2
- 或者,只需搜索 ; JVRavichandran"
http://www.Google.com

***通过开发人员指南 http://www.developersdex.com ***
不要只是参加USENET ......获得奖励!



Ravichandran,


我刚发现这篇文章,看起来相当不错。
http://www.codeproject.com/csharp/De...sAndEvents.asp


如果我在阅读之后仍然感到困惑,我会告诉你的 - 到目前为止我已经跟着它了b $ b!你看,我有一个VB背景,所以学习如何正确编程
是一个有点陡峭的曲线! :-)


-

Mark Allison,SQL Server MVP
http://www.markallison.co.uk

寻找SQL Server复制书?
http://www.nwsu.com/0974973602m.html

Ravichandran JV写道:

我今天肯定会在这两篇文章上发一篇简短的文章,但同时在
中,我很确定你会觉得这很有帮助。 br />
委托是一种能够封装方法签名的类型。代表可以与事件一起使用。事件
是一种通常在类中声明的类型。

公共类MyClass
{
//指向要调用的方法的委托。在这种情况下,
//方法的返回类型是''void''。
public delegate void MyDelegate();
//如果要处理事件的方法
//(本例中为MyMethod)接受参数,声明
//委托如下
public delegate void MyDelegate(int i);
//事件类型MyDelegate
公共事件MyDelegate MyEvent;
public MyClass()
//将事件附加到代表
this.MyEvent + = new MyDelegate(MyMethod);
}
//这是充当事件过程的方法。
public void MyMethod()
//
//在此处理事件。
}
}


JVRavichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry = Ravichandran + JV& cob = aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID = P3966388& BN = 999& PN = 2
- 或者,只需搜索 ; JVRavichandran"
http://www.Google.com

***通过开发人员指南 http://www.developersdex.com ***
不要只是参加USENET ......获得奖励!



Hi,

I am new to C# and want to create my own events within a console app.
However I am struggling mentally with this, and have looked at the MSDN
documentation but I am drawing a blank. Is there anywhere on the net
that shows a very basic delegate and a very basic event? Some nice
simple example code is what I''m looking for with no fluff around it.

Thanks!

--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html

解决方案

I will definitely post a simple article on both today but, in the
meanwhile, I am quite sure you should find this helpful.

The delegate is a type which is capable of encapsulating a method
signature. The delegate can be used in conjunction with events. An event
is a type that is usually declared inside the class.
public class MyClass
{
// The delegate that points to the method to invoke. The
// method''s return type in this case is ''void''.
public delegate void MyDelegate();
// If the method that is going to handle the event
// (MyMethod in this case) takes parameters, declare
// the delegate as follows
public delegate void MyDelegate(int i);
// The event is of type MyDelegate
public event MyDelegate MyEvent;
public MyClass()
{
// Attach the event to the delegate
this.MyEvent+=new MyDelegate(MyMethod);
}
// This is the method that acts as the event procedure.
public void MyMethod()
{
// Handle the event here.
}
}

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


Thanks,

OK, let me put this into my own words, and then you can slap me down. ;-)

You create a delegate called public delegate void MyDelegate(int i) -
this delegate runs code with the same sig. Cool, got that.

You then define your event as follows:
public event MyDelegate MyEvent;

I don''t acutally understand the above line of code. What is that
effectively declaring?

When you attach the event to the delegate are you "firing the event",
raising the event? I want the event to be listened to from another class
altogether from this one. How do I get that other class to listen to
events in this class?

I''m sorry but your very simple example just confused me even more.
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Ravichandran J.V. wrote:

I will definitely post a simple article on both today but, in the
meanwhile, I am quite sure you should find this helpful.

The delegate is a type which is capable of encapsulating a method
signature. The delegate can be used in conjunction with events. An event
is a type that is usually declared inside the class.
public class MyClass
{
// The delegate that points to the method to invoke. The
// method''s return type in this case is ''void''.
public delegate void MyDelegate();
// If the method that is going to handle the event
// (MyMethod in this case) takes parameters, declare
// the delegate as follows
public delegate void MyDelegate(int i);
// The event is of type MyDelegate
public event MyDelegate MyEvent;
public MyClass()
{
// Attach the event to the delegate
this.MyEvent+=new MyDelegate(MyMethod);
}
// This is the method that acts as the event procedure.
public void MyMethod()
{
// Handle the event here.
}
}

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



Ravichandran,

I just found this article, looks quite good.
http://www.codeproject.com/csharp/De...sAndEvents.asp

I will let you know if I''m still confused after reading that - I''m
following it so far! You see, I have a VB background, so learning how to
program properly is a bit of a steep curve! :-)

--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Ravichandran J.V. wrote:

I will definitely post a simple article on both today but, in the
meanwhile, I am quite sure you should find this helpful.

The delegate is a type which is capable of encapsulating a method
signature. The delegate can be used in conjunction with events. An event
is a type that is usually declared inside the class.
public class MyClass
{
// The delegate that points to the method to invoke. The
// method''s return type in this case is ''void''.
public delegate void MyDelegate();
// If the method that is going to handle the event
// (MyMethod in this case) takes parameters, declare
// the delegate as follows
public delegate void MyDelegate(int i);
// The event is of type MyDelegate
public event MyDelegate MyEvent;
public MyClass()
{
// Attach the event to the delegate
this.MyEvent+=new MyDelegate(MyMethod);
}
// This is the method that acts as the event procedure.
public void MyMethod()
{
// Handle the event here.
}
}

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



这篇关于学习代表和活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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