为什么不编译? [英] Why doesn't this compile?

查看:59
本文介绍了为什么不编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到这个问题时,我需要在某些代码中使用自定义事件订阅和取消订阅方法

我的应用程序。

我创建了这个简单的类来演示编译问题。

使用TestSuccess事件编译,但TestFailure的使用不行。

这对我来说没有任何意义。

谁能告诉我我做错了什么,或者是什么?


class TestEvent

{

public delegate void TestSuccessEventHandler(string message);

公共事件TestSuccessEventHandler TestSuccess;

public void SendSuccessEvent()

{

if(TestSuccess!= null)

TestSuccess(测试成功);

}

public delegate void TestFailureEventHandler (字符串消息);

公共事件TestFailureEventHandler TestFailure

{

add

{

//在这做点什么

TestFailure + =值;

}

删除

{

TestFailure - = value;

//做点什么

}

}

public void SendFailureEvent()

{

if(TestFailure!= null)

TestFailure(" Test Failure" );

}

}

解决方案

Scott S.< Sc****@community.nospamwrote:


我需要在某些代码中使用自定义事件订阅和取消订阅方法

我的应用程序当我遇到这个问题时。

我创建了这个简单的类来说明编译问题。

使用TestSuccess事件编译,但TestFailure的使用不会。

这对我来说没有任何意义。

谁能告诉我,我做错了什么,或者是什么?



字段式事件 (比如TestSuccess)声明一个事件和一个

变量 - 变量是在类中引用的变量,并且

该事件是在类外引用的。


现在,你已经宣布了TestFailure事件但没有相应的

变量。即使你的代码被编译了,当你试图订阅这个事件时,你会很快收到堆栈溢出,因为它只会再次调用

订阅者。你需要一个普通的变量来实际持有

委托实例,而*那个'是*添加/删除的主体和

SendFailureEvent应该引用的内容。 />

请参阅 http:// pobox。 com / ~siget / csharp / events.html 了解更多详情。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

英国的世界级.NET培训: http:// iterativetraining .co.uk


12月6日,13:49,Scott S.< Sco ... @ community.nospamwrote:
< blockquote class =post_quotes>
我需要自定义事件订阅和取消订阅方法当我遇到这个问题时,我的应用程序中的一些代码是



我创建了这个简单的类来说明编译问题。

使用TestSuccess事件编译,但TestFailure的使用没有。

这对我来说没有任何意义。

谁能告诉我我的意思你做错了,或者是什么?


class TestEvent

{

public delegate void TestSuccessEventHandler(string message);

公共事件TestSuccessEventHandler TestSuccess;

public void SendSuccessEvent()

{

if(TestSuccess!= null)

TestSuccess(测试成功) };

}


public delegate void TestFailureEventHandler(string message);

公共事件TestFailureEventHandler TestFailure

{

add

{

//在这做点什么

TestFailure + = value;

}

删除

{

TestFailure - =值;

//在这做点什么

}

}

public void SendFailureEvent()

{

if( TestFailure!= null)

TestFailure(测试失败);

}

}



请尝试下面的代码。我无法真实地描述它为什么会出错,它与编译器对访问权限的作用有关。我是

确定有人可以解释


我添加了一些线程安全功能以便进行测量:)


使用系统;

名称空间WindowsApplication34

{

class TestEvent

{

public delegate void TestSuccessEventHandler(string message);

公共事件TestSuccessEventHandler TestSuccess;

public void SendSuccessEvent()

{

if(TestSuccess!= null)

TestSuccess(测试成功);

}

public delegate void TestFailureHandler (对象发送者,EventArgs

e);


私有事件TestFailureHandler _testFailure;

private readonly对象_eventLock = new object();


公共事件TestFailureHandler TestFailure

{

add

{

锁定(_eventLock)

{

//在这做点什么

_testFailure + = value;

}

}

删除

{

锁定(_eventLock)

{

_testFailure - = value;

//做点什么

}

}

}

protected virtual void OnSendFailure(EventArgs e)

{

//如果为空则不要开火(这意味着没有人订阅。

if(_testFailure!= null)

{

//开火活动

_testFailure(this,e);

}

}

public void SendFailureEvent()

{

OnSendFailure(new EventArgs( )); //测试

}

}

}


12月6日,14日:14,Jon Skeet [C#MVP]< sk ... @ pobox.comwrote:


Scott S.< Sco ... @ community.nospamwrote:


当我遇到这个问题时,我需要在我的应用程序的某些代码中使用自定义事件订阅和取消订阅方法。

我创建了这个简单的类来说明编译问题。

使用TestSuccess事件编译,但TestFailure的使用不会。

它对我来说没有任何意义。

谁能告诉我,我做错了什么,或者是什么?



A类似字段的事件 (比如TestSuccess)声明一个事件和一个

变量 - 变量是在类中引用的变量,并且

该事件是在类外引用的。


现在,你已经宣布了TestFailure事件但没有相应的

变量。即使你的代码被编译了,当你试图订阅这个事件时,你会很快收到堆栈溢出,因为它只会再次调用

订阅者。你需要一个普通的变量来实际持有

委托实例,而*那个'是*添加/删除的主体和

SendFailureEvent应该引用的内容。 />

Seehttp://pobox.com/~skeet/csharp/events.html了解更多详情。


-

Jon Skeet - < sk ... @ pobox.com> http:// www。 pobox.com/~skeet 博客: http://www.msmvps。 com / jon.skeet

英国的世界级.NET培训: http://iterativetraining.co.uk



好​​解释:)


I need to have custom event subscribe and unsubscribe methods in some code in
my application when I ran into this problem.
I created this simple class to demostrate the compile problem.
The use of TestSuccess event compiles, but the use of TestFailure doesn''t.
It doesn''t make any sense to me why.
Can anyone tell me what I''m either doing wrong, or what?

class TestEvent
{
public delegate void TestSuccessEventHandler( string message );
public event TestSuccessEventHandler TestSuccess;
public void SendSuccessEvent()
{
if( TestSuccess != null )
TestSuccess( "Test Success" );
}
public delegate void TestFailureEventHandler( string message );
public event TestFailureEventHandler TestFailure
{
add
{
// Do Something Here
TestFailure += value;
}
remove
{
TestFailure -= value;
// Do Something Here
}
}
public void SendFailureEvent()
{
if( TestFailure != null )
TestFailure( "Test Failure" );
}
}

解决方案

Scott S. <Sc****@community.nospamwrote:

I need to have custom event subscribe and unsubscribe methods in some code in
my application when I ran into this problem.
I created this simple class to demostrate the compile problem.
The use of TestSuccess event compiles, but the use of TestFailure doesn''t.
It doesn''t make any sense to me why.
Can anyone tell me what I''m either doing wrong, or what?

A "field-like event" (like TestSuccess) declares both an event and a
variable - the variable is what gets referred to within the class, and
the event is what gets referred to outside the class.

Now, you''ve declared the TestFailure event but no corresponding
variable. Even if your code compiled, you''d get a stack overflow as
soon as you tried to subscribe to the event, because it would just call
the subscriber again. You need a normal variable to actually hold the
delegate instance, and *that''s* what the body of add/remove and
SendFailureEvent should refer to.

See http://pobox.com/~skeet/csharp/events.html for more details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


On 6 Dec, 13:49, Scott S. <Sco...@community.nospamwrote:

I need to have custom event subscribe and unsubscribe methods in some code in
my application when I ran into this problem.
I created this simple class to demostrate the compile problem.
The use of TestSuccess event compiles, but the use of TestFailure doesn''t.
It doesn''t make any sense to me why.
Can anyone tell me what I''m either doing wrong, or what?

class TestEvent
{
public delegate void TestSuccessEventHandler( string message );
public event TestSuccessEventHandler TestSuccess;
public void SendSuccessEvent()
{
if( TestSuccess != null )
TestSuccess( "Test Success" );
}

public delegate void TestFailureEventHandler( string message );
public event TestFailureEventHandler TestFailure
{
add
{
// Do Something Here
TestFailure += value;
}
remove
{
TestFailure -= value;
// Do Something Here
}
}
public void SendFailureEvent()
{
if( TestFailure != null )
TestFailure( "Test Failure" );
}
}

Try the code below. I can''t really describe why it goes wrong, it''s
something to do with what the compiler does with access rights. I''m
sure someone will be able to explain

I added some thread safety features for good measure :)

using System;
namespace WindowsApplication34
{
class TestEvent
{
public delegate void TestSuccessEventHandler( string message );
public event TestSuccessEventHandler TestSuccess;
public void SendSuccessEvent()
{
if( TestSuccess != null )
TestSuccess( "Test Success" );
}
public delegate void TestFailureHandler(object sender, EventArgs
e);

private event TestFailureHandler _testFailure;
private readonly object _eventLock = new object();

public event TestFailureHandler TestFailure
{
add
{
lock(_eventLock)
{
// Do Something Here
_testFailure += value;
}
}
remove
{
lock(_eventLock)
{
_testFailure -= value;
// Do Something Here
}
}
}
protected virtual void OnSendFailure(EventArgs e)
{
//Don''t fire if null (which means no one is subscribed.
if (_testFailure != null)
{
//Fire the event
_testFailure(this,e);
}
}
public void SendFailureEvent()
{
OnSendFailure(new EventArgs()); //test
}
}
}


On 6 Dec, 14:14, Jon Skeet [C# MVP] <sk...@pobox.comwrote:

Scott S. <Sco...@community.nospamwrote:

I need to have custom event subscribe and unsubscribe methods in some code in
my application when I ran into this problem.
I created this simple class to demostrate the compile problem.
The use of TestSuccess event compiles, but the use of TestFailure doesn''t.
It doesn''t make any sense to me why.
Can anyone tell me what I''m either doing wrong, or what?


A "field-like event" (like TestSuccess) declares both an event and a
variable - the variable is what gets referred to within the class, and
the event is what gets referred to outside the class.

Now, you''ve declared the TestFailure event but no corresponding
variable. Even if your code compiled, you''d get a stack overflow as
soon as you tried to subscribe to the event, because it would just call
the subscriber again. You need a normal variable to actually hold the
delegate instance, and *that''s* what the body of add/remove and
SendFailureEvent should refer to.

Seehttp://pobox.com/~skeet/csharp/events.htmlfor more details.

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk

Good explanation :)


这篇关于为什么不编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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