| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1240 人关注过本帖
标题:委托中的参数
只看楼主 加入收藏
rgbtdkjcel
Rank: 1
等 级:新手上路
帖 子:294
专家分:0
注 册:2007-3-26
结帖率:50%
收藏
 问题点数:0 回复次数:6 
委托中的参数

using System;

public delegate void NameListEventHandler(object source, WindowsApplication5.NameListEventArgs args);

namespace WindowsApplication5
{
/// <summary>
/// Summary description for NameListEventArgs.
/// </summary>
public class NameListEventArgs : EventArgs
{
string name;
int count;

public NameListEventArgs()
{
//
// TODO: Add constructor logic here
//
}

public NameListEventArgs(string name, int count)
{
this.name = name;
this.count = count;
}

public string Name
{
get
{
return name;
}
}

public int Count
{
get
{
return count;
}
}
}
}

public delegate void NameListEventHandler(object source, WindowsApplication5.NameListEventArgs args);

这里的object source,windowsapplication5.namelisteventargs args这两个参数是什么地方获取实参呢??

为什么windowsapplication5.namelisteventargs 要继承eventargs类呢???

搜索更多相关主题的帖子: 参数 public summary 委托 constructor 
2007-09-27 13:54
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 

哪个对象触发了它.那么source就是那个对象.args就是里面包含的所有事件

至于为什么要继承,因为eventargs是他的一个基类啊


有实力才会有魅力 实力来自坚持不懈的努力
2007-09-27 14:01
jxnuwy04
Rank: 2
等 级:新手上路
威 望:4
帖 子:768
专家分:0
注 册:2006-9-15
收藏
得分:0 
楼上正解!

------------------不为别的,就为你,我的理想!-----------------
2007-09-27 14:18
rgbtdkjcel
Rank: 1
等 级:新手上路
帖 子:294
专家分:0
注 册:2007-3-26
收藏
得分:0 


private void button1_Click(object sender, System.EventArgs e)
{
NameList names =new NameList();
names.nameListEvent += new NameListEventHandler(NewName);
names.nameListEvent += new NameListEventHandler(CurrentCount);

names.Add("MSDN");
names.Add("Webcast");
}


这里的sender是不是指button1呢???这里的e是不是指click事件呢??


c sharp初学者
2007-09-27 14:24
jxnuwy04
Rank: 2
等 级:新手上路
威 望:4
帖 子:768
专家分:0
注 册:2006-9-15
收藏
得分:0 

不错,sender是指button1,但由于System.EventArgs是包含事件数据的类的基类,此类不包含事件数据.在事件引发时不向事件处理程序传递状态信息的事件会使用此类,e就是此类的一个对象.我现在还没有更深的理解.


------------------不为别的,就为你,我的理想!-----------------
2007-09-27 14:56
rgbtdkjcel
Rank: 1
等 级:新手上路
帖 子:294
专家分:0
注 册:2007-3-26
收藏
得分:0 
names.nameListEvent += new NameListEventHandler(NewName);


像这样为事件添加了一个响应方法.但是这里new NameListEventHandler(NewName);是一个委托。即然是委托。。就自然得引用委托。。
例如:
public delegate void test(string int);

delegate one=new test(方法名);
one(参数);//来引用委托。。


但是在上例中。。该如何来引用委托呢???

c sharp初学者
2007-09-27 16:02
rgbtdkjcel
Rank: 1
等 级:新手上路
帖 子:294
专家分:0
注 册:2007-3-26
收藏
得分:0 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "View";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
NameList names =new NameList();
names.nameListEvent += new NameListEventHandler(NewName);
names.nameListEvent += new NameListEventHandler(CurrentCount);

names.Add("MSDN");
names.Add("Webcast");
}

public static void NewName(object source, NameListEventArgs args)
{
Console.WriteLine(args.Name + "was added to the list");
}

public static void CurrentCount(object source, NameListEventArgs args)
{
Console.WriteLine("list currently has " + args.Count + " items.");
}
}
}


全部代码


c sharp初学者
2007-09-27 16:06
快速回复:委托中的参数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015050 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved