当前位置:首页>网络学院>程序开发>c#教程>文章内容

使用C#轻松编写.Net组件(3)

[ 来源:www.it55.com | 作者: | 时间:2007-07-14 | 收藏 | 推荐 ] 【

     然后,我们编写了一个GetString()函数,这个函数根据用户传入的index值,返回相应的记录:
  public string GetString(int index)
  { … return StringsSet[index];}
  要注意的是其中的异常处理的方法:
  throw new IndexOutOfRangeException();
  作为一个健壮的组件,异常处理机制是不可或缺的,虽然它可能会消耗掉一些资源,但是它带来的安全性的提升会使你觉得消耗的资源简直微不足道。这里使用了一个系统定义的异常类IndexOutOfRangeException(),事实上,更多的情况是你必须自己定义异常类,以适应各种不同的情况。下面的代码示例展示了如何定义一个异常类:
  public class MyApplicationException : ApplicationException{ public string AMsg;
  public MyApplicatonException(string strMsg) sflj www.it55.com kg^&fgd
  {
  AMsg=strMsg;
  }
  }
  定义一个异常类与定义普通的类并没有什么区别,唯一的区别在于异常类必须继承自System.Exception类。事实上,微软公司推荐把所有用户自定义的异常类作为ApplicationException类的子类。把类MyApplicationException放到命名空间CompCS中,这样你就可以改写GetString()函数中的异常处理方式。下面是一个带有更完善的异常处理机制的GetString()方法:
  public string GetString(int index) { try { if ((index < 0) || (index >= StringsSet.Length)) { throw new MyApplicationException("参数超出范围"); } } catch(MyApplicationException mErr) { Console.WriteLine(mErr.AMsg); } catch(Exception Err) { Console.WriteLine(Err.Message); }
  return StringsSet[index];
  }
  采用类似这样的方式,你可以应付比这复杂得多的情况。
  下面,我们来考虑给这个类添加事件。事件机制的引入使得开发者可以更灵活地开发程序。下面的代码示例展示了如何定义一个事件:
  public event EventHandler Modified; it55.com
  在C#中使用event关键字定义事件。把这个定义放到我们的类ComponentCS.StringComponent中,然后我们添加一个函数Modify(),这个函数修改字符数组StringsSet中指定位置的值,同时引发OnModify事件,而在Modify事件中,我们调用的是事件Modified所指定的函数:
  public void Modify(int index,string value) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } else { StringsSet[index]=value; OnModify(); } }
  private void OnModify()
  {
  EventArgs e=new EventArgs();
  if(!(Modified==null))
  Modified(this,e);
  }
  然后我们可以用如下的方法调用:
  private void DoIt(){ StringComponent mysc=new StringComponent(); mysc.Modified+=new EventHandler(Called); mysc.Modify(2,"another string");}public void Called(object o,EventArgs e){ Console.WriteLine("Changed");}
  在函数DoIt()中,我们首先建立了一个StringComponent类的对象mysc,然后将它的Mofidied事件关联到Called()方法: it55.com
  mysc.Modified+=new EventHandler(Called);
  注意“+=”符号的使用,相反地,如果使用“-=”符号,可以取消这个事件的绑定。
  现在我们得到了一个虽然简单,但是比较完整的组件类:
  using System;
  namespace ComponentCS
  {
  public class StringComponent
  {
  private string[] StringsSet;
  public event EventHandler Modified;
  public int StringLength
  {
  get
  {
  return StringsSet.Length;
  }
  }
  public void Modify(int index,string value)
  {
  if ((index < 0) || (index >= StringsSet.Length))
  {
  throw new IndexOutOfRangeException();
  }
  else
  {
  StringsSet[index]=value;
  OnModify(); IT资讯之家 www.it55.com
  }
  }
  private void OnModify()
  {
  EventArgs e=new EventArgs();
  if(!(Modified==null))
  Modified(this,e);
  }
  public StringComponent()
  {
  StringsSet = new string[]
  {
  "C# String 0",
  "C# String 1",
  "C# String 2",
  "C# String 3"
  };
  }
  public string GetString(int index)
  {
  if ((index < 0) || (index >= StringsSet.Length))
  {
  throw new IndexOutOfRangeException();
  }
  return StringsSet[index];
  }
  }
  }
  最后要做的就是把它编译成.dll(动态链接库)文件,以便发布。发布成.dll文件最大的好处就是.dll文件中的内容已经编译,可以大大加快程序运行速度,此外还可以保护源代码。

(编辑:IT资讯之家 www.it55.com

返回顶部
共2页: 上一页 1 [2] 下一页  

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]

图片文章