2006-09-01

几个C#编程的小技巧

来源: 中国本站网 作者:佚名 评论 0 条
 
(3)在某个Method或者Property内部某处,触发事件
(4)Client程序中使用 =操作符指定事件处理程序

例子: // 定义Delegate类型,约束事件程序的参数
public delegate void MyEventHandler(object sender, long lineNumber) ;

public class DataImports
{
// 定义新事件NewLineRead
public event MyEventHandler NewLineRead ;

public void ImportData()
{
long i = 0 ; // 事件参数
while()
{
i ;
// 触发事件
if( NewLineRead != null ) NewLineRead(this, i);
//...
}
//...
}
//...
}

// 以下为Client代码

private void CallMethod()
{
// 声明Class变量,不需要WithEvents
private DataImports _da = null;
// 指定事件处理程序
_da.NewLineRead = new MyEventHandler(this.DA_EnterNewLine) ;
// 调用Class方法,途中会触发事件
_da.ImportData();
}
// 事件处理程序
private void DA_EnterNewLine(object sender, long lineNumber)
{
// ...
}

三、IP与主机名解析
使用System.Net可以实现与Ping命令行类似的IP解析功能,例如将主机名解析为IP或者反过来: private string GetHostNameByIP(string ipAddress)
{
IPHostEntry hostInfo = Dns.GetHostByAddress(ipAddress);
return hostInfo.HostName;
}
private string GetIPByHostName(string hostName)
{
System.Net.IPHostEntry hostInfo = Dns.GetHostByName(hostName);
return hostInfo.AddressList[0].ToString();
}

http://www.cnblogs.com/xh831213/archive/2006/08/31/491425.html


共2页: 上一页 [1] 2 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:解决JSP开发Web程序中文显示的三种方法  
下一篇:JSP的特点和其它动态网页开发技术比较
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号