委托,事件和事件处理程序的命名约定? [英] Naming convention for delegates, events and event handlers?

查看:75
本文介绍了委托,事件和事件处理程序的命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力确定实现活动所需的3件事物的好命名约定



你需要:

1)代表

2)活动

3)活动处理程序


以下是我的了解基于Windows

Forms事件的命名约定。如果这个惯例是标准的话,有人可以告诉我,

或者合适或者只是一般的错误。


干杯


Boz


delegate void DataAvailableEventHandler(字符串数据);


类ServerClass

{

公共事件DataAvailableEventHandler DataAvailable;


public FireEvent()

{

if(DataAvailable!= null)

{

DataAvailable(" bang");

}

}

}


类ClientClass

{

public ClientClass(ServerClass服务器)

{

server.DataAvailable + = new

DataAvailableEventHandler(ServerClass_DataAvailabl e);

}


private ServerClass_DataAvailable(字符串数据) )

{

Trace.WriteLine(我有一些数据:\ n+ +数据);

}

}

解决方案

Boz,


#3并不是那么重要,因为这是一个私人实现,并且

命名约定不与私人成员打交道(毕竟,它们是

私有,而不是暴露)。


你有1和2的命名约定。我唯一的东西是
比如你的事件处理程序应该遵循事件的模式

已经建立。


这意味着你有两个参数。第一个是object类型,

命名为sender,它是触发事件的对象的实例。


第二个是派生自的类EventArgs(或EventArgs本身,

,如果没有额外信息)。


在这种情况下,你会有一个名为DataAvailableEventArgs的类

公开了一个名为Data的属性,它会暴露你的字符串。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< jj ******** @ yahoo.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...

我正在努力确定实施活动所需的3件事的良好命名惯例。

您需要:
1)代表< 2)事件
3)事件处理程序

以下是我对基于Windows
Forms事件的命名约定的理解。如果这个惯例是标准的,或者合适的或者只是一般的错误,有人可以告诉我。

Boz

Boz

委托无效DataAvailableEventHandler(字符串数据);

类ServerClass
{
公共事件DataAvailableEventHandler DataAvailable;

公共FireEvent()
{
if(DataAvailable!= null)
{DataAvailable(" bang");
}
}
}
类ClientClass
公共ClientClass(ServerClass服务器)
{/ server.DataAvailable + = new
DataAvailableEventHandler(ServerClass_DataAvailabl e);
}
<私有ServerClass_DataAvailable(字符串数据)
{Trace>WriteLine(我有一些数据:\ n+ +数据);
}
}


< jj ******** @ yahoo.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...

我正在努力确定实现活动所需的3件事的良好命名约定。




FxCop有一个设置您可以在哪里审核您的命名约定。


如果您没有它,您可以在

http://www.gotdotnet.com/team/fxcop/


- Alan


Hello Boz


不确定你是否知道它,但2.0框架提供了通用

EventHandler。


公共事件EventHandler< DataAvailableEventArgs> DataAvailable;


我最近重构了一个2.0的框架,并用如上所示的通用2.0模式替换了所有

特定于事件处理程序的代理。


-

关于

Anders Borum / SphereWorks

微软认证专家(.NET MCP)

I''m trying to pin down a good naming convention for the 3 things
required to implement an event.

You need:
1) a delegate
2) an event
3) an event handler

Below is my understanding of a naming convention based on the Windows
Forms events. Can someone let me know if this convention is standard,
or suitable or just plain wrong.

Cheers

Boz

delegate void DataAvailableEventHandler(string data);

class ServerClass
{
public event DataAvailableEventHandler DataAvailable;

public FireEvent()
{
if (DataAvailable != null)
{
DataAvailable("bang");
}
}
}

class ClientClass
{
public ClientClass(ServerClass server)
{
server.DataAvailable += new
DataAvailableEventHandler(ServerClass_DataAvailabl e);
}

private ServerClass_DataAvailable(string data)
{
Trace.WriteLine("I got some data:\n" + data);
}
}

解决方案

Boz,

#3 isn''t so important, because that is a private implementation, and the
naming conventions don''t deal with private members (after all, they are
private, and not exposed).

You have the naming conventions right for 1 and 2. The only thing I
would say is that your event handler should follow the pattern for events
already established.

This means that you have two parameters. The first is of type object,
named sender, which is the instance of the object that fired the event.

The second is a class that derives from EventArgs (or EventArgs itself,
if there is no extra information).

In this case, you would have a class named DataAvailableEventArgs which
exposes a property named Data, which would expose your string.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<jj********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

I''m trying to pin down a good naming convention for the 3 things
required to implement an event.

You need:
1) a delegate
2) an event
3) an event handler

Below is my understanding of a naming convention based on the Windows
Forms events. Can someone let me know if this convention is standard,
or suitable or just plain wrong.

Cheers

Boz

delegate void DataAvailableEventHandler(string data);

class ServerClass
{
public event DataAvailableEventHandler DataAvailable;

public FireEvent()
{
if (DataAvailable != null)
{
DataAvailable("bang");
}
}
}

class ClientClass
{
public ClientClass(ServerClass server)
{
server.DataAvailable += new
DataAvailableEventHandler(ServerClass_DataAvailabl e);
}

private ServerClass_DataAvailable(string data)
{
Trace.WriteLine("I got some data:\n" + data);
}
}



<jj********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

I''m trying to pin down a good naming convention for the 3 things
required to implement an event.



FxCop has a setting where you can audit your naming conventions.

If you don''t have it you can get it at

http://www.gotdotnet.com/team/fxcop/

-- Alan


Hello Boz

Not sure you''re aware of it, but the 2.0 framework provides a generic
EventHandler.

public event EventHandler<DataAvailableEventArgs> DataAvailable;

I recently refactored a framework for 2.0 and replaced all
eventhandler-specific delegates with the generic 2.0 pattern as seen above.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)


这篇关于委托,事件和事件处理程序的命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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