vb.net类型转换_将VB6转换为VB.NET

vb.net类型转换

1.0 - Introduction1.0-简介

Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET).

将Visual Basic 6.0(VB6)转换为Visual Basic 2008+(VB.NET)。

If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting VB6 to VB.NET seem to think that because these two languages have the same name (at least in part) that they are the same language; that is, VB.NET is just VB6 'NETicised'.  Maybe all you have to do is open the VB6 project in a .NET version of Visual Studio and the conversion wizard will do it all for you...

如果有一个充满浮躁和错误决定的话题,那就是这个! 第一个问题似乎是正在考虑将VB6转换为VB.NET的任务的人们似乎认为,因为这两种语言具有相同的名称(至少部分地具有相同的名称),因此它们是相同的语言。 也就是说,VB.NET只是VB6'NETicized'。 也许您要做的就是在.NET版本的Visual Studio中打开VB6项目,转换向导将为您完成所有工作...

That thinking is wrong!  The two are very different languages, with completely different behaviours.

这种想法是错误的! 两种语言截然不同,行为却完全不同。

2.0 - Notice2.0-注意

In this article I have assumed that the version of the IDE being used is Visual Studio 2008 at a minimum, with V3.0 of the .NET Framework.

在本文中,我假定所使用的IDE版本至少是Visual Studio 2008,以及.NET Framework V3.0。

3.0 - Some Important Differences3.0-一些重要的区别

There are a number of significant differences between the way VB6 and VB.NET work. These differences could create unexpected behaviour in the code if not dealt with. Some of these are outlined in this section.

VB6和VB.NET的工作方式之间有许多重大差异。 如果不加以处理,这些差异可能会在代码中创建意外行为。 本节概述了其中一些。

3.1 - Fixed Length Strings

3.1-定长字符串

Unlike VB6, VB.NET does NOT support fixed length strings. You will probably find a VB6 code declaration of:

与VB6不同,VB.NET不支持固定长度的字符串。 您可能会发现以下VB6代码声明:

strTranDate As String * 8  ‘ a fixed length string 8 bytes long
 Public strTranDate() As Char

If you are writing position-dependent code, then you must explicitly add logic to check the string and truncate it if necessary (or treat as an error) to avoid inappropriate behaviour.

如果要编写位置相关的代码,则必须显式添加逻辑以检查字符串,并在必要时截断字符串(或将其视为错误),以避免不当行为。

3.2 - Buffer Allocation

3.2-缓冲区分配

Array indexing in VB6 starts from 1 rather than from 0 as it does in the .NET framework.

VB6中的数组索引从1开始而不是从.NET框架开始的0

In order to preserve backward compatibility, VB.NET implements buffer allocation in a broken way -- it will actually allocate an extra element (something that does not happen in C#).  Thus, when you declare a Char buffer of a set size, VB.NET will create a buffer one byte larger.

为了保持向后兼容性,VB.NET以中断的方式实现了缓冲区分配-实际上它将分配一个额外的元素(这在C#中是不会发生的)。 因此,当您声明一个设置大小的Char缓冲区时,VB.NET将创建一个大一个字节的缓冲区。

For instance, in VB6 the following code

例如,在VB6中,以下代码

    Dim MyBuffer as new Char(6) {}

However, in VB.NET, it will create a 7-byte array, with a starting prefix of zero.

但是,在VB.NET中,它将创建一个7字节的数组,起始前缀为零。

3.3 - Error Numbers

3.3-错误编号

Error numbers may vary.   In some cases, error codes returned by VB.NET may be different than those returned by VB6 Visual Basic 6.0.  For error handling code that relied on the values returned by Err.Number, this might cause different (and unwanted) behaviour in your application. For example:

错误号可能会有所不同。 在某些情况下,VB.NET返回的错误代码可能与VB6 Visual Basic 6.0返回的错误代码不同。 对于依赖于Err.Number返回的值的错误处理代码,这可能会导致应用程序中的行为不同(并且不希望出现)。 例如:

‘ Visual Basic 6.0
On Local Error GoTo ErrorHandle
Dim x() As Boolean
Dim y As Variant
	
y = x(10)
	
ErrorHandle:
     If Err.Number = 9 Then
          ' Do something
     Else
           ' Do something else
     End If

3.4 - Late-bound Calls to COM Objects May Cause Type Mismatch Errors

3.4-对COM对象的后期绑定调用可能会导致类型不匹配错误

In VB6, when a late-bound COM object is passed as a parameter to a late-bound call, the object is coerced to a Variant of type Nothing.

在VB6中,当将后期绑定的COM对象作为参数传递给后期绑定的调用时,该对象将被强制为Nothing类型的Variant。

When upgraded to VB.NET, COM objects declared as type Object are treated the same as Variants (which are always converted to type Object during upgrade).  These objects are marshalled to the variant type Empty.  This causes a type mismatch error in VB.NET.

升级到VB.NET时,将声明为Object类型的COM对象与Variants对待(在升级过程中始终将其转换为Object类型)。 这些对象被编组为变量类型Empty 。 这会在VB.NET中导致类型不匹配错误。

To avoid this problem, make sure that all objects are early bound.

为避免此问题,请确保所有对象都已提前绑定。

3.5 - Closing a Form Calls Dispose

3.5-关闭表单调用处理

In VB6, you can unload a form and later reload it by calling the Show method.

在VB6中,您可以卸载表单,然后再通过调用Show方法来重新加载它。

In VB.NET, the Close method for a form calls the Dispose method so that it is automatically garbage-collected.  This can cause subtle behavioural differences that may be hard to detect.

在VB.NET中,窗体的Close方法调用Dispose方法,以便自动对其进行垃圾收集。 这可能会导致细微的行为差异,可能难以发现。

In Visual Basic 2008, if you call the Show method for a form that has been unloaded, you will get a new instance of the form; any property settings from the base class will be lost.

在Visual Basic 2008中,如果为已卸载的表单调用Show方法,则将获得该表单的新实例;否则,您将获得该表单的新实例。 基类的所有属性设置都将丢失。



For forms that are shown modally, Dispose is not called automatically. In some cases, you may want to call Dispose in order to clean up resources

对于以模态显示的表单,不会自动调用Dispose。 在某些情况下,您可能需要致电Dispose来清理资源



翻译自: https://www.experts-exchange.com/articles/3883/Converting-VB6-to-VB-NET.html

vb.net类型转换

你可能感兴趣的:(vb.net类型转换_将VB6转换为VB.NET)