请教委托和事件的初级问题!

anantnt203120 2004-06-30 06:18:02
请问以下这种委托和时间的声明是什么意思?
public delegate void NextEventHandler(object sender, EventArgs e);
public event NextEventHandler Next;

接下来他又这样用:
if(Next!=null)
{
Next(sender,new EventArgs());
}

还有:
public StartPanel(ReservationForm containerForm):this()

请问:this 这是什么意思?方法继承自己所属的类?
菜鸟对这些概念不太理解。
...全文
216 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Samen168 2004-07-30
  • 打赏
  • 举报
回复
//定义事件委托
public delegate void NextEventHandler(object sender, EventArgs e);
//定义NextEventHandler委托类型的事件
public event NextEventHandler Next;

接下来他又这样用:
if(Next!=null) //如果Next有被预定则触发Next事件(若为null触发会出错)
{
Next(sender,new EventArgs());
}

还有:
public StartPanel(ReservationForm containerForm):this()
这句的意思是在调用本类的 public StartPanel()构造函数
还可以用 public StartPanel(ReservationForm containerForm,object value):this(containerForm)
lxcc 2004-07-30
  • 打赏
  • 举报
回复
this是当前类的隐含实例!
superhasty 2004-07-30
  • 打赏
  • 举报
回复
up一下。
javabeanzhang 2004-07-30
  • 打赏
  • 举报
回复
1、 delegate void NextEventHandler(object sender, EventArgs e);

定义一个NextEventHandler类型的委托,
public event NextEventHandler Next;

定义一个NextEventHandler类型的委托实例,并且是事件(特殊类型的委托)
if(Next!=null)
{
Next(sender,new EventArgs());
}
如果实例有相关联的方法,就执行
2、
this()在调用之前先调用同名的不带参数的函数



我们可以回顾一下以往的事件触发机制:
当某个事件触发, 亦即具体到某个函数被调用, 该函数往往需要通知对该事件感兴趣的客户代码, 以往的做法通过遍历一个函数指针链表, 执行相关的函数来完成. 因此客户代码只需在初始化的时候把自己的事件处理函数注册到这个函数指针链表中就可以了, 当事件发生, 自己的时间处理函数必定被执行到.
.net使用代理简化了这个步骤
public delegate void NextEventHandler(object sender, EventArgs e);声明告诉编译器你的代理对型如(object sender, EventArgs e)的函数感兴趣
而public event NextEventHandler Next;则声明了一张可以往里填充型如(object sender, EventArgs e)的函数的哈希表.
if(Next!=null) //判断Next里面是否注册了可调用的函数
{
Next(sender,new EventArgs()); //执行被注册的函数
}

楼上两位说的很对。小弟引用一下。呵呵
补充一下:
调用if(Next!=null)
{
Next(sender,new EventArgs());
}
前,加入事件函数到匹配的委托中,你就不会这样迷惑了
Next += new NextEventHandler(Eventfunction);


chinawn 2004-07-30
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3168/3168824.xml?temp=.5505945
anantnt203120 2004-07-30
  • 打赏
  • 举报
回复
谢谢大家!
liduke 2004-07-15
  • 打赏
  • 举报
回复
1、 delegate void NextEventHandler(object sender, EventArgs e);

定义一个NextEventHandler类型的委托,
public event NextEventHandler Next;

定义一个NextEventHandler类型的委托实例,并且是事件(特殊类型的委托)
if(Next!=null)
{
Next(sender,new EventArgs());
}
如果实例有相关联的方法,就执行
2、
this()在调用之前先调用同名的不带参数的函数
xuguchu 2004-07-15
  • 打赏
  • 举报
回复
當你
a myobja=new a(10);
時,系統先運行默認構造函数public a()
(沒有參數的constructor,如果自己沒有定義,就自動執行系統的默認構造函数).
然后再執行blic a(int b)
運行后下如你所見哦
現在明白啦吧
呵呵
接分
xuguchu 2004-07-15
  • 打赏
  • 举报
回复
:this()
是調用默認構造函数,也就是主調對象的默認構造函数.
見下例吧
using System;

public class a
{
public a()
{
Console.WriteLine("No Params");
}
public a(int b):this()
{
Console.WriteLine("Class a's value of b is :"+b);
}
}

namespace IO
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class ShowFile
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
a myobja=new a(10);

}

}
}
你先調試一下這個程式,看結果你就明白啦
KentYu 2004-07-15
  • 打赏
  • 举报
回复
public delegate void NextEventHandler(object sender, EventArgs e)
定义一个事件委托,与一般委托相比事件委托有两个特点:1.它的返回类型是void 2.总有两个参数,第一个是事件源,第二个是EventArgs或它的派生类的一个实例

public event NextEventHandler Next
创建了事件委托的实例
anantnt203120 2004-07-15
  • 打赏
  • 举报
回复
top
superct 2004-07-15
  • 打赏
  • 举报
回复
我们可以回顾一下以往的事件触发机制:
当某个事件触发, 亦即具体到某个函数被调用, 该函数往往需要通知对该事件感兴趣的客户代码, 以往的做法通过遍历一个函数指针链表, 执行相关的函数来完成. 因此客户代码只需在初始化的时候把自己的事件处理函数注册到这个函数指针链表中就可以了, 当事件发生, 自己的时间处理函数必定被执行到.
.net使用代理简化了这个步骤
public delegate void NextEventHandler(object sender, EventArgs e);声明告诉编译器你的代理对型如(object sender, EventArgs e)的函数感兴趣
而public event NextEventHandler Next;则声明了一张可以往里填充型如(object sender, EventArgs e)的函数的哈希表.
if(Next!=null) //判断Next里面是否注册了可调用的函数
{
Next(sender,new EventArgs()); //执行被注册的函数
}
anantnt203120 2004-06-30
  • 打赏
  • 举报
回复
回复人: SoRoMan(因为所以,所以因为) ( ) 信誉:100 2004-06-30 19:46:00 得分: 0


delegate,event,这些只是语法规定。看看MSDN。
:this()调用本类的constructor.



public StartPanel(ReservationForm containerForm):this()
这里的StartPanel就是该类的重载构造函数啊。
:this()还是不明白是什么意思?

anantnt203120 2004-06-30
  • 打赏
  • 举报
回复
我只知道
public delegate void NextEventHandler(object sender, EventArgs e);
public event NextEventHandler Next;

if(Next!=null)
{
Next(sender,new EventArgs());
}
是响应一个输入框的回车事件,执行下一步的操作(Next()).
但Next(sender,new EventArgs())是什么意思?

anantnt203120 2004-06-30
  • 打赏
  • 举报
回复

private void idText_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13)
{
if(Next!=null)
{
Next(sender,new EventArgs());
}
}
}

private void firstnameRequestText_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13)
{
if(Next!=null)
{
Next(sender,new EventArgs());
}
}
}

private void lastnameRequestText_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13)
{
if(Next!=null)
{
Next(sender,new EventArgs());
}
}
}


#region IReservationPanel Members

public string Title
{
get
{
return "Locate Customer";
}
}

public string Description
{
get
{
return "Please type ID or name of the customer";
}
}

public int Initiate()
{
return 1;
}

public int Complete()
{
collCustomers_Item[] customersTemp;

if(idText.Text=="" && lastnameRequestText.Text=="" && firstnameRequestText.Text=="")
{
MessageBox.Show("customer id or name must type.","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
return 0;
}

if(idText.Text!="")
{
Cursor=Cursors.WaitCursor;
customersTemp=Interface.RequestCustomers(idText.Text);
Cursor=Cursors.Default;
}
else
{
Cursor=Cursors.WaitCursor;
customersTemp=Interface.RequestCustomersName(firstnameRequestText.Text,lastnameRequestText.Text);
Cursor=Cursors.Default;
}

if(customersTemp==null)
{
if(idText.Text!="")
{
idText.SelectAll();
idText.Focus();
}
else
{
firstnameRequestText.SelectAll();
firstnameRequestText.Focus();
}
return 0;
}

mainForm.customers=customersTemp;
return 1;
}

#endregion

public void SetReservationForm(ReservationForm containerForm)
{
mainForm=containerForm;
}
marvelstack 2004-06-30
  • 打赏
  • 举报
回复
namespace ConsoleTest
{
delegate void delegate1(string str);
delegate void delegate2(int dd);
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
delegate1 d1= new delegate1(StrMethod);
delegate2 d2= new delegate2(IntMethod);
d1("委托包装字符串方法");
d2(111111);

Console.ReadLine();
}
public static void StrMethod(string str)
{
Console.WriteLine(str);
}
public static void IntMethod(int dd)
{
Console.WriteLine(dd.ToString());
}

}
}

委托可以看作安全的函数指针;
写一个函数
public string GetValue(int i)
{
i.ToString();
}
//定义委托
delegate string GetValuehandler(int i);
//使用安全包装
GetValuehandler mydele = new GetValuehandler(GetValue);
//执行
string s = mydele(99);
相当于执行了
string s = GetValuehandler(99);
anantnt203120 2004-06-30
  • 打赏
  • 举报
回复
完整的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using OpenCourse.Rental.XCustomer;
using OpenCourse.Rental.XRentalReservation;

namespace OpenCourse.Rental
{
/// <summary>
/// Summary description for StartPanel.
/// </summary>
public class StartPanel : System.Windows.Forms.UserControl, IReservationPanel
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label firstnameRequestLabel;
private System.Windows.Forms.TextBox firstnameRequestText;
private System.Windows.Forms.Label lastnameRequestLabel;
private System.Windows.Forms.TextBox lastnameRequestText;
private System.Windows.Forms.Label idLabel;
private System.Windows.Forms.TextBox idText;
private ReservationForm mainForm;

public delegate void NextEventHandler(object sender, EventArgs e);
public event NextEventHandler Next;

public StartPanel()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call

}
public StartPanel(ReservationForm containerForm):this()
{
mainForm=containerForm;
}
SoRoMan 2004-06-30
  • 打赏
  • 举报
回复
delegate,event,这些只是语法规定。看看MSDN。
:this()调用本类的constructor.
tenglong2004 2004-06-30
  • 打赏
  • 举报
回复
给个完整代码先

110,569

社区成员

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

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

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