会员: 密码:  免费注册 | 忘记密码 | 会员登录 网页功能: 加入收藏 设为首页 网站搜索  
 安全技术技术文档
  · 安全配制
  · 工具介绍
  · 黑客教学
  · 防火墙
  · 漏洞分析
  · 破解专题
  · 黑客编程
  · 入侵检测
 安全技术论坛
  · 安全配制
  · 工具介绍
  · 防火墙
  · 黑客入侵
  · 漏洞检测
  · 破解方法
  · 杀毒专区
 安全技术工具下载
  · 扫描工具
  · 攻击程序
  · 后门木马
  · 拒绝服务
  · 口令破解
  · 代理程序
  · 防火墙
  · 加密解密
  · 入侵检测
  · 攻防演示
技术文档 > VB文档 > VB.Net
VB2005语言新功能
发表日期:2005-09-24 09:34:08作者: 出处:  

有以下主要新功能

运算符重载/部分类/Continue 关键字/Using 关键字/拆分存取访问/泛型支持


运算符重载
 Dim ShortSpan, LongSpan, TotalSpan As TimeSpan
 ShortSpan = TimeSpan.FromMinutes(100)
 LongSpan = TimeSpan.FromDays(14)
 TotalSpan = ShortSpan + LongSpan
 Console.WriteLine(TotalSpan.TotalMinutes)
 为什么可以直接TotalSpan = ShortSpan + LongSpan这样呢,原因是TimeSpan类对+号运算符进行了重载,如
  Public Shared Operator+(objA As MyClass,
    objB as MyClass) As MyClass
      ' (Code goes here.)
  End Operator

部分类
 所谓部分类,就是把类定义为类的一部分,一个类可以有多个部分类组成而形成一个完整的类
 部分类的主要作用用于把类的定义分别存于不同的文件中,使用如下
 比如在文件TestClass_1.vb中存放下面代码
 Partial Public Class 存放
    Public Sub TestMethodA()
        Console.WriteLine("Method A called.")
    End Sub
 End Class
 在文件TestClass_2.vb中存放下面代码
 Partial Public Class TestClass
     Public Sub TestMethodB()
         Console.WriteLine("Method B called.")
     End Sub
 End Class

  而调用时和存放在一起没有什么区别
  Dim Obj As New TestClass()
 Obj.TestMethodA()
 Obj.TestMethodB()

 
Continue 关键字
 有3中使用方法
 Continue For、Continue Do 和 Continue While
 分别应用于
 For ... Next, Do ... Loop, or While ... End While中
 如下例子
 For i = 1 to 1000
    If i Mod 5 = 0 Then
        ' (Task A code.)
        Continue For
    End If
    ' (Task B code.)
 Next

Using 关键字
 Using表达式用于控制资源释放
 如下:
 Using NewFile As New _
   System.IO.StreamWriter("c:\MyFile.txt")
     NewFile.WriteLine("This is line 1")
     NewFile.WriteLine("This is line 2")
 End Using
 
 ' The file is closed automatically.
 ' The NewFile object is no longer available here.
 这样,当使用了using关键字时,VB2005会自动调用对象的Dispose()方法释放资源

拆分存取访问
 在以前有以下三种访问方式
 公用 (Public)
 友元 (Friend)
 私用 (Private)
 但是,当碰到下面情况时会碰到一下问题,如只想给访问Status的权限,而不让非友元类设置Status的值
  Public Property Status() As Integer
     Get
         Return _Status
     End Get
     Set(ByVal value As Integer)
         _Status = value
     End Set
  End Property
 这时就比较难以处理了,然而在VB.2005中可以使用如下代码实现
 Public Property Status() As Integer
     Get
         Return _Status
     End Get
     Friend Set(ByVal value As Integer)
         _Status = value
     End Set
 End Property

泛型支持 
 泛型支持在C++中已经得到广泛应用,现在VB.2005也开始支持泛型
 和泛型对照的一个非常实用的类是System.Collections.ArrayList类,它是一个可以自动调整大小的集合,
 它可以存放一般的.NET对象,为了支持这项功能,它把所有对象看出OBJECT对象,
 这样,当你只想用它来存放某一个类别的对象时,你不能保证它不能存放其他不同的类别
 然而在泛型中,可以通过OF关键字来强行确定类型,从而保证了容器存放组件的一致性,如下
  Public Class GenericList(Of ItemType)
      Inherits CollectionBase
 
      Public Function Add(ByVal value As ItemType) _
        As Integer
          Return List.Add(value)
      End Function
 
      Public Sub Remove(ByVal value As ItemType)
          List.Remove(value)
      End Sub
 
      Public ReadOnly Property Item( _
        ByVal index As Integer) As ItemType
          Get
              ' The appropriate item is retrieved from
              ' the List object and explicitly cast to
              ' the appropriate type, and then returned.
              Return CType(List.Item(index), ItemType)
          End Get
      End Property
  End Class
 调用
 ' Create the GenericList instance, and
 ' choose a type (in this case, string)
 Dim List As New GenericList(Of String)
 
 ' Add two strings.
 List.Add("blue")
 List.Add("green")
 
 ' The next statement will fail because it has the
 ' wrong type. There is no automatic way to convert
 ' a Guid object to a string. In fact, this line
 ' won't ever run, because the compiler notices the
 ' problem and refuses to build the application.
 List.Add(Guid.NewGuid())
   
 
.NET2。0提供下面的泛型供使用,你可以在Systems.Collections.Generic 命名空间中找到这些泛型
List – 和 GenericList 例子类似的基本集合
Dictionary –用于索引名称=值的集合
LinkedList –链表
Queue – 先进先出集合
Stack – 后进先出集合
ReadOnlyCollection – 有固定项目集的集合,其中項目一旦建立就无法更改
SortedDictionary – 永远保持排序的名称-值集合

 

 

返回顶部】 【打印本页】 【关闭窗口

关于我们 / 给我留言 / 版权举报 / 意见建议 / 网站编程QQ群   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net 加载时间 0.00169