VB2005语言新功能[转]
2006-03-05 14:41
417 查看
运算符重载/部分类/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 – 永远保持排序的名称-值集合
---------------------------------------------------------------------
今天用了一下using关键字
Using reader As XmlReader = XmlReader.Create(booksFile, settings)
While (reader.Read())
If (reader.NodeType = XmlNodeType.Element _
And “book” = reader.LocalName) Then
bookcount += 1
End If
End While
End Using因为xmlreader封装了IDisposable接口,所以可以用Using关键字,当xmlReader读完xml文件时,执行Dispose()方法。
运算符重载
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 – 永远保持排序的名称-值集合
---------------------------------------------------------------------
今天用了一下using关键字
Using reader As XmlReader = XmlReader.Create(booksFile, settings)
While (reader.Read())
If (reader.NodeType = XmlNodeType.Element _
And “book” = reader.LocalName) Then
bookcount += 1
End If
End While
End Using因为xmlreader封装了IDisposable接口,所以可以用Using关键字,当xmlReader读完xml文件时,执行Dispose()方法。
相关文章推荐
- 新增百万测试数据之 程序语言+导入功能
- 使用Java语言开发微信公众平台(八)——自定义菜单功能
- .NET framework 4.5新特性预览:核心语言 新增 功能和改进
- 转: KindEditor 图片空间文件增加删除文件、文件夹功能(ASP语言环境)
- 网络爬虫:利用有道实现“语言翻译”功能
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现Timer计时功能
- 易语言功能演示+官方演示
- 微软计划在开发平台集成F#语言 基于功能编程概念
- C++语言运算符的功能、优先级和结合性
- 第八课,T语言功能和参数(版本5.0)
- [SQL]SQL语言入门级教材_SQL功能与特性(一)
- c语言课程设计常用功能
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 使用 Java 8 语言功能
- 学习MySQL_第三天_语言基础&数据定义功能
- Visual Studio 2010新特性:动态语言功能
- InstallShield 2012 Spring新功能试用(20): Suite/Advanced UI工程支持添加语言
- C# 2.0 语言和编译器中的新增功能
- C语言中strlen函数功能及用法
- 【脚本语言系列】关于 Python 日志功能,你需要知道的事