【VB/.NET】Converting VB6 to VB.NET 【Part II】【之二】

前端之家收集整理的这篇文章主要介绍了【VB/.NET】Converting VB6 to VB.NET 【Part II】【之二】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第二部分

原文

Variables and Attributes
Most VB6 variable types have equivalent types in VB.NET,but there are a couple of twists. An Integer in VB6 is a 16-bit integer; in VB.NET,it is a 32-bit integer. The 16-bit equivalent in .NET is a Short,which is what the wizard will convert it to. Sometimes this is correct,but in cases where the 16-bit values reflect a VB6 limitation that does not exist in VB.NET,it makes more sense to upgrade the variable to the VB.NET 32 bit Integer. The preferred way to do this is to change the VB6 Integer to a VB6 Long (32-bit integer) before the conversion. A complete list of equivalent data types not only for VB6 and VB.NET,but also IDL (COM),the .NET framework,and MSIL (.NET assembly language) can be found at(Note From the Author: This URL will only work if Visual Studio is installed on your computer)ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dv_vstechart/html/ vbtchTroubleshootingNETInteroperability.htm.

The VB6 Date type is a simple 8-byte variable type; VB.NET uses the .NET DateTime class for the same functionality. In places where the old 8-byte VB6 type is needed,use the DateTime.Date functions FromOADate(x) or ToOADate(x). The wizard will make use of these functions when converting; for example,MyDate = 100 will be upgraded to MyDate = DateTime. Date.FromOADate(100). I have read that the VB6 Date type is one thing that just will not convert to .NET; these functions have done the few things I have required of them,so I suspect that those who say Date types are hard to upgrade are just not aware of these functions. There is so much in .NET that these mistakes are easy to make. When I first converted the MSChart control,I thought the Axis property had not converted (see Part 1 of this series),and spent a lot of time looking for a replacement control. This was not a total waste of time,as I switched to a pure .NET control,which I would have done eventually; but this did delay having working code for the first conversion pass. The lesson to be learned from this is that if you do not think a function exists in .NET,keep looking,you just might find it.

Another type that changes is the VB6 Currency type,which converts to a VB.NET Decimal type. I have heard this can cause issues,but the few tests I have run show them behaving the same.

Attributes are new to .NET. In VB6,a procedure had three things: visibility (public/private),scope (local/global),and type (Integer,etc). .NET adds an arbitrary number of optional attributes; these can be used by the compiler or by the program at runtime (or both). The COMVisible attribute is an example of an attribute used by the compiler at runtime. When added to a class,property,or function,it tells the compiler to create code that will allow that member to be accessed by legacy COM applications. You can create your own custom attributes (it is similar to creating a class),and check at runtime to see if it has been added to a function. Why and how you would do this is outside the scope of this article. The .NET framework provides a number of useful attributes,a few of which will be used later in this article.


译文:

变量和属性

大多数VB6的变量类型在VB.NET中会有对应等价的变量类型,但也有那么一两个奇葩。一个Integer在VB6中是一个16位的整型,在VB.NET中是一个32位的。向导在转换的时候会将转换为.NET中等同的16位的Short。有时候向导确实干了一件好事,但是万一这个16位值想要表达的VB6中的限值(边界问题)在VB.NET却没有,这就更意味着要将这个变量升级到VB.NET中32位的Integer。建议在转换之前先将VB6的Integer换成Long(VB6中为32b整型)。有一个完整的等效数据类型列表不仅包括了VB6和VB.NET的内容,还有IDL(COM),.NET framework,以及MSIL(译者注:.NET汇编语言 关于.NET汇编语言,请点击 ),这个列表在ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dv_vstechart/html/ vbtchTroubleshootingNETInteroperability.htm 可以被找到(作者注:这种连接在你安装的VS里面才能使用,其实就是MSDN帮助文档)

在VB6中Date数据类型是一种简单的8字节变量,而.NET中设计了DateTime类来达到使用的目的。在需要原来的八字节类型的地方,使用可返回DateTime.Date类型的函数FromOADate(x) 或者 ToOADate(x).向导在升级转换的时候就是会使用这些函数,例如,语句MyDate = 100 将变为MyDate = DateTime.Date.FromOADate(100)。我曾经看到有人说VB6的DATE类型不会被转换到.NET的,但我认为这些函数都能做到我所需要的功能,所以我很怀疑那些说DATE类型升级很困难的人一定是不知道这些个函数,对于.NET这些错误的认识实在是很常见而且很容易出错。当我刚开始做MSChart控件的转换时,我认为坐标轴属性没有转换(见该系列Part I),而且我还花了很多时间去寻找一个控件来替代。但是这并不是完全在浪费时间,因为我最终还是会换到一个纯粹的.NET控件来完成代码。但是这就推迟了上文提到的代码转换的测试。其实我想说的意思就是,如果你不认为一个函数会在.NET中存在,那也请别放弃寻觅,你也许会找到她。

另一个会发生变化的类型就是Currency ,这将被转换成一个VB.NET的Decimal类型。据说转换会带来问题,但是经过几个测试后发现他们运行起来没什么区别。

特性是.NET中新添加的东西(译者注:置于这是什么,详情点击)。在VB6里,一个过程有三个重要的东西:可见性(public/private),作用域(local/global),类型(Integer 等)。.NET还增加了任意多个可选特性。这些特性可以在编译时和运行时(反射技术)被使用。COMVisible 特性就是一个运行时被使用的一个很好的例子。当要添加到一个类、属性、或者函数当中去时,他就会告知编译器创建相应的代码来使得原先的COM应用程序可以访问该成员。也可以创建你自己的自定义特性(这类似于创建一个类),在运行时检查下看它是否已经被添加到一个函数里头。至于为什么,怎么做,这已经超出了本文的范围了(译者注:通过IL,能够反射出来)。.NET框架提供了许多有用的特性,其中一些在这篇文章中将会被提及。


Update Log:

2014-5-5 23:20

猜你在找的VB相关文章