C#事件引用问题

cch1010 2010-12-24 05:10:18
EventHandler中代码

using system;
namespace EventHandler
{
public delegate int RunHandler(object sender, EventArgs e);
}


Test 中代码

using System;
using EventHandler;
namespace Test
{
public class A
{
protected event RunHandler OnBeforeFunctionRun;

protected virtual int ExecuteSubFunction(object sender, EventArgs e)
{
return 0;
}

}


}


txt 中代码

using System;
using Test;
using EventHandler;
namespace txt
{
public class B : A
{
protected override int ExecuteSubFunction(object sender, EventArgs e)
{
if (this.OnBeforeFunctionRun != null)
{
int iRetValue = OnBeforeFunctionRun(sender, e);
if (iRetValue != 0)
{
return iRetValue;
}
else
{
return -1;
}
}
}
}

}


最后一段代码如何修改才能不报错?
...全文
188 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
SuperTyro 2010-12-27
  • 打赏
  • 举报
回复
出现的错误 是不是 不是所有路径都有返回值?
k0mmDu 2010-12-27
  • 打赏
  • 举报
回复
另外不声明RunHandler类型的事件OnBeforeFunctionRun,直接声明RunHandler委托的实例可以避免这样的问题.
test中改成

public class A
{
//protected event RunHandler OnBeforeFunctionRun;
protected RunHandler OnBeforeFunctionRun;
protected virtual int ExecuteSubFunction(object sender, EventArgs e)
{
return 0;
}

}


k0mmDu 2010-12-27
  • 打赏
  • 举报
回复
lovelan1748 2010-12-24
  • 打赏
  • 举报
回复
1.你事件名称定义有问题,一般以ed或ing结尾。
2.用protected作为事件的修饰符貌似没有意义
3.你的错误信息应该贴出来,我看是因为没有返回值的问题,在if外return一个值就行了
dujunqiang2010 2010-12-24
  • 打赏
  • 举报
回复
c#中要自定义事件,这个对象必须继承EventArgs才能实现
tanrenzong1986 2010-12-24
  • 打赏
  • 举报
回复
事件一般似用委托调用的。
fxfeixue 2010-12-24
  • 打赏
  • 举报
回复
public class A
{
protected event RunHandler OnBeforeFunctionRun;

protected virtual int ExecuteSubFunction(object sender, EventArgs e)
{
return 0;
}

// ========增加方法=======
protected int RaiseEvent(object sender, EventArgs e)
{
if (OnBeforeFunctionRun != null)
{
int iRetValue = OnBeforeFunctionRun(sender, e);
if (iRetValue != 0)
{
return iRetValue;
}
else
{
return -1;
}
}

return -1;
}
// ======================
}

public class B : A
{
protected override int ExecuteSubFunction(object sender, EventArgs e)
{
return RaiseEvent(sender, e);
}
}
caitlin_yu 2010-12-24
  • 打赏
  • 举报
回复
private void 。。_MouseDown(object sender, MouseEventArgs e)
{
if (e.Clicks == 1 && e.Button == MouseButtons.Left)
{
。。
}
我的事件是这样调用的~~
xugan666 2010-12-24
  • 打赏
  • 举报
回复
public class B : A
{
protected override int ExecuteSubFunction(object sender, EventArgs e)
{
if (this.OnBeforeFunctionRun != null)
{
int iRetValue = OnBeforeFunctionRun(sender, e);
if (iRetValue != 0)
{
return iRetValue;
}
else
{
return -1;
}
}
}
}

}

大概看了你的代码。
protected override int ExecuteSubFunction(object sender, EventArgs e)

虽然你继承了。但是(object sender, EventArgs e) 还是会出现问题的。
用,看一下参数。。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧