将你的Visual Basic 6.0移植到Visual Basic.NET(4)

  • 摘要:本文推荐给那些准备将自己的应用移植到Visual Basic.NET中的Visual Basic开发者。文章中包括Visual Basic.NET升级工具信息的介绍以及将Visual Basic 6结构平滑升级到Visual Basic.NET 的指导。
  • 标签:.NET  Basic.NET  Basic  升级

继承
假设你编写了一个只包含一个函数的名称为BaseClass的基本类:
Function GetCustomerName()
注释: Do some stuff
End Function

如果你想建立一个新类,并使用BaseClass中的GetCustomerName函数。但有不想重写一编代码。那么通过继承,你可以这样编写新类:

Inherits BaseClass
Function GetCustomerID()
注释: Do some stuff
End Function

重载  
重载就是建立两个具有同样你名称但是参数属性不同的函数的过程。假设有GetCustomID函数根据输入输出用户的ID号码。那么该函数可能有以下两种类型:

Function GetCustomerID(custname as string) As Integer
注释: 通过用户名获得用户ID
End Function

Function GetCustomerID(purch as long) As Integer
注释: 根据购物单号码获得用户ID
End Function

通过重载,你就可以根据不同类型的输入调用同一个函数。
实际上现在的VB在某种程度上提供了重载的功能,考虑下面的函数:

Function GetCustomID(vIN As Variant) As Long
GetCustomID = vIN
End Function

你可以以下面两种方式调用

ID = GetCustomID("123")

或者
ID = GetCustomID(123)

那么为什么还要在新版本中引进重载呢?这是因为新的Visual Basic引进了新的特性:类型安全(default type safety)。在Visual Basic.NET中,Variant类型将不复存在,详细的介绍看下面关于升级部分的内容。

多态
多态是在一个类中重新定义一个函数的过程。例如你需要建立一个基于BaseClass类的子类,但是又需要重新编写GetCustomerName函数,在新版的VB中,你就可以这样写:

Inherits BaseClass

Function GetOrders()
Overrides Function GetOrders()
...
End Function

线程
新的Visual Basic语言结构部分将包含建立多线程,一个线程建立的范例如下:

set t = New Thread(New Threadstart(AddressOf(BaseClass.Function1))

从上面的范例还可以看到,Visual Basic.NET中的AddressOf函数将真正返回函数的指针。这在进行API调用,例如回调函数方面将是很有帮助的。

错误处理(Error Handling)
Visual Basic.NET的另外一个大的改进就是错误处理。Visual Basic将提供try…catch…finally结构,就象Delphi中的那样(微软的原文是:just like those "respectable" languages :-))。例如:

Sub SafeWrite()
Try
Open "Testfile"
...
Write #1
Catch
Kill "Testfile"
Finally
Close #1
End Try
End Sub

升级
当代码升级后,Visual Basic.NET会建立一个新的升级工程。下面通过几个例子来说明你的代码做了那些升级。

Variant类型到Object类型
以前版本的Visual Basic支持Variant数据 

Integer数据类型到Short数据类型   
在Visual Basic 6.0中,16bit数现在称为Short。32bit数称为Integer(64bit数称为Long)。当工程升级后,数据定义会做如下改动:

Dim x As Integer
dim y as Long

更改为:

Dim x As Short
dim y as Integer

属性语法   
Visual Basic.NET对于自定义属性的语法作乐更加直观的更改,将Get和Set合而为一。例如原来的属性如下:

Property Get MyProperty() As Integer
m_MyProperty = MyProperty
End Property
Property Let MyProperty(NewValue A

(责任编辑 火凤凰 sunsj@51cto.com  TEL:(010)68476636-8007)



共4页: 上一页 [1] [2] [3] 4
【内容导航】
专题:ASP.NET 2.0基础开发指南
.NET移动与嵌入式技术专题
.NET Framework新手入门专题
VS.NET实用开发专题
ADO.NET实用技巧专题
 
 验证码: (点击刷新验证码)   匿名发表
  • Visual C++ 完全自学宝典

  • 作者:强锋科技,朱洪波
  • Visual C++ 6.0是微软公司为程序人员提供的Visual Studio 6.0工具套件中的重要组成部分。本书由浅入深地介绍使用Visual C++ 6.0..
Copyright©2005-2008 51CTO.COM 版权所有