登录社区:用户名: 密码: 忘记密码 网页功能:加入收藏 设为首页 网站搜索  

文档

下载

图书

论坛

安全

源码

硬件

游戏
首页 | 信息 | 空间 | VB | VC | Delphi | Java | Flash | 补丁 | 控件 | 安全 | 黑客 | 电子书 | 笔记本 | 手机 | MP3 | 杀毒 | QQ群 | 产品库 | 分类信息 | 编程网站
  立华软件园 - Visual Basic 专区 - 技术文档 - VB.Net 技术文章 | VB源代码 | 电子图书 | VB网站 | 相关下载 | 在线论坛 | QQ群组 | 搜索   
 VB技术文档
  · 窗体界面
  · 系统控制
  · VB.Net
  · 多媒体
  · 网络编程
  · API函数
  · 游戏编程
  · 数据报表
  · 其他文档
 VB源代码
  · 窗体界面
  · 文件目录
  · 多媒体
  · 网络编程
  · 系统API
  · 数据报表
  · 游戏编程
  · VBA办公
  · 其他代码
 VB论坛
  · Visual Basic 讨论区
  · VB.Net 讨论区
  · VB数据库开发讨论区
  · VB系统API讨论区
 其他VB资源
  · VB下载资源
  · VB电子图书
  · VB QQ群组讨论区
  · VB 其他网站资源




VB2005语言新功能
发表日期:2005-09-24作者:[转贴] 出处:  

有以下主要新功能

运算符重载/部分类/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 – 永远保持排序的名称-值集合

 

 

我来说两句】 【发送给朋友】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 VB2005语言新功能

 ■ [欢迎对本文发表评论]
用  户:  匿名发出:
您要为您所发的言论的后果负责,故请各位遵纪守法并注意语言文明。

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放 / 友情链接  
Copyright ©2001-2006 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.0019