【VB/.NET】Converting VB6 to VB.NET 【Part I】【之一】

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

前段时间一直打算要翻译这篇文章。现在终于找到空来撰写了。

整篇文章篇幅较长,这里分一部分一部分翻译,尽量做到用词精准,有不准的或者异议请一定提出!


原文在我的文章http://www.jb51.cc/article/p-puhdwmrr-zq.html 里有副本。


第一部分

原文:

Before We Even Start

Remove dead code; you cannot have trouble with code you do not convert. Remove variables that are never used and subroutines that are never called. I converted one application that was large enough to have a number of associated programs,such as configuration programs,format converters,upgrade tools,and some ActiveX controls. Most of these utilities shared header files (WIN32 API declare statements and user type definitions) with the main application,but most of these programs only used a few of the functions in the headers. Other applications did not use anything in some of the headers. By removing the unused files and functions from the project,I was able to convert all the utilities much quicker. This allowed some functionality to be shown quickly,and provided the experience needed to tackle the main application. If the VB6 program has variable names that match keywords in VB.NET,they'll be converted with '_renamed' appended to the declaration. The wizard doesn't always find where the variable is used,so the best option is to change the variable name before the conversion.

Don't bother with formatting code in the pre-conversion stage - the converter will do its own formatting during the conversion.


Well-Written Code Converts Well

I was around when Borland and then Microsoft released C++ compilers,and I was responsible for converting a lot of C code to C++. One lesson I learned was that well-written code converted far easier than poorly written code. So,the first task of a conversion effort should to clean up and simplify the original code. The following checklist contains some of the pre-conversion steps that will make the conversion easier - I will be covering all of these.
  1. Declare all variables.
  2. Give all variables explicit type,not variants.
  3. Do not worry about code formatting.
  4. Do not use 0 to disable timer,set enable = false instead (vb6 default is 0,enabled).
  5. Can't be perfect (bring to front).
  6. Do an internal test and release of a clean VB version before the final conversion.
  7. Declare only one variable per line.
  8. Remove dead code.
  9. Set the startup object to a form.
  10. Make sure the code compile. and runs before the conversion.
  11. Convert all arrays to 0 based.
  12. Remove all "as any."
  13. Convert VB1 - VB5 to VB6 first.
  14. Convert DAO and RDO to ADO.
  15. Do not use defaults when accessing control properties.
  16. Rename any VB.NET keywords in the VB6 code.

When I was doing the C to C++ conversions,one key quality indicator that I looked for was function prototypes. In C,prototypes were optional (and didn't even exist in the early days of C!). In C++,prototypes are mandatory. When I saw C code with prototypes,the conversion typically went well; when they were missing,other issues often came up during the conversion.

A similar quality indicator in VB6 is the declaration of variables and variable types. VB6 allows untyped variables in the form of variants. Actually,VB6 doesn't even require variables to be declared. In most cases,using variants is a bad programming practice; not declaring variables is always a bad programming practice.

So,the first thing to do when converting VB6 code to VB.NET is to add the Option Explicit to all code files,ensuring that all variables are declared. Next,specify all variable types,eliminating all variants that really only hold a single type. In many cases,it's obvIoUs what type a variable should be,but incorrectly changing a variant that should be an Integer,Long,Single,or Double to a different type can cause bugs that are difficult both to detect and troubleshoot. This is one reason why I recommend a complete testing cycle after preparing VB6 code for conversion,but before doing the final conversion. If you can add more tests for corner cases in any math routines to your test suite,it would probably be worth it.

Another good practice is to declare one variable per line. Dim a,b,c As Integer creates two Variants and one Integer,not the three Integers you might expect. This error is more likely to cause problems in VB.NET than in VB6.

If your code is in an earlier version of VB,convert it to VB6 first. Next,if you use DAO or RDO to access databases,rewrite it to use ADO (more on this in the next article). VB.NET 2003 does a better job of converting VB than the original VB.NET,so if you're going to convert to .NET,it makes sense to skip VB.NET 2002 and convert straight to VB.NET 2003. Because of this,I won't cover converting VB.NET 2002,but most of the information given here will still apply if you choose that route.

After the upgrade,the new .NET project will be in its own folder,with the original VB6 project unchanged,so you can do as many trial runs as you like,and use the upgrade reports as guides to issues to correct before doing the final conversion. The update report is an html file listing files and the errors in each file.


译文

Before We Even Start开始之前要说的话

移除死代码(/无用的代码);你不能被这些不需要转换的死代码所困扰。删除从来没有使用过的变量和不会被调用的Sub。我(原文作者,下同)转换过一个具有比如配置程序、格式转换器、升级工具和一些ActiveX控件这些相关程序组成的足够大的应用程序。大多数这些实用的工具(配置程序等)都与主应用程序共享着许多头文件(包括WIN32 API的声明语句与用户类型定义),但是大多数类似于这样的程序(我所转换过的)仅仅使用头文件里的一小部分函数。而其它的程序(不同于main程序)却不使用这些头文件(被引用、使用过的)中的任何东西。 通过从项目中移除这些不使用的文件,我就能更快速地转换这些实用工具。这样以来,一些程序中的功能就会变得很显眼,并且能够提供一些转换主程序(其实应该就是main函数)的非常必要的经验。


如果VB6中一些变量的名字与VB.NET中的关键字有冲突,这些变量名在声明时将被转换为带有追加后缀'_renamed'的样子。但是这个向导(代码升级向导)并不会查到你到底在哪里使用了这个变量(,意思是这些不被查找的地方将会发生错误的转换,由'_renamed'引起的错误),所以你最好的选择是在转换之前更改这些变量名称

不要在预转换阶段干扰代码的格式化--因为在转换的时候转换器会自顾自地格式化的。/(这句感觉翻译的不是很到位,各位达人觉得呢?)


Well-Written Code Converts Well 写得好的代码自然转换得也好。

当Borland 以及之后 Microsoft也发布了C++编译器,我很徘徊,同时我又要负责许多C TO C++的代码转换。 我从中学到的是,编写良好的代码转换较写得不好的代码容易得多。因而,转换工作的首要任务是要清理以及简化原来的代码。下面列出的是预转换所要进行的步骤,可以使得转换更加容易些--我这些步骤都会去做。

1、声明所有的变量(有Option Explicit 的基本上不用担心此类问题)。

2、明确定义变量类型,而不是变体(variants)。

3、不要为代码的格式化。

4、不要使用0(interval)来关闭timer,而是要通过设置enable=false来完成。

5、提前说下,转换并不是完美的。

6、在最终转换之前做一个内部测试并且生成一个干净的VB6版release

7、每行只做一个变量的声明

8、去除代码

9、给form设置启动项(/这个不知道为什么要这样,有谁能解释下嘛?)

10、在转换之前,确保代码成功编译与运行。

11、转换所有的数组为标号从0开始的。

12、移除所有的"as any"。

13、先将VB1-VB5的代码都转换到VB6。

14、将DAO和RDO转换到ADO

15、不要使用缺省属性链接到控件属性(如:TEXT1="XiaoY_H" ,应该改成 TEXT1.TEXT="XiaoY_H")

16、在VB6代码重命名会与VB.NET里关键字冲突的名称

当我在做C TO C++转换的时候,我所关注的关键质量指标就是函数原型.在C中,原型是随意的可选的(甚至早期的C中根本就不存在!)。在C++中,原型却是强制的。当我看到具有原型的C代码时,转换通常进行地很顺利。而当原型漏掉了,转换过程中往往出现一些其它问题。

在VB6当中同样有着这样的质量指标,那就是变量及其类型的声明。VB6允许未定义类型的变量以变体的形式存在着。事实上,VB6甚至不需要声明变量(就可以使用了)。在大多数情况下,使用变体是一种糟糕透了的编程习惯,不声明变量同样如此。

所以,在转换VB6 TO VB.NET时首先要做的事情就是将Option Explicit增加到所有的代码文件中,以此来确定所有的变量都被声明了。接着,明确指定所有的变量类型,"消灭"掉所有能用一个单一类型表示的变体。在许多情况下,一个变量应该是什么类型的是很明显的事情,但是,错误地将一个应该是整型、长整型、单精度或者双精度的变体改成另一个不同的类型会给检测和排除故障带来困难

这便是我建议在为转换准备好VB6代码后要进行一个完整的测试周期,但是要在最终转换之前。如果你能在测试套件里的任何math routines为Corner Cases增加更多的测试,这将会可能很有价值。

另一个好习惯就是每行只声明一个变量。Dim a,c As Integer实际上声明了两个变体和一个整型,而并不是你可能希望的三个整型。这样的错误在VB.NET中比VB6中更有可能引起问题。

如果你的代码是基于早期的VB版本,请先将其转换到VB6.接着,如果你使用DAO或者RDO来访问数据库,请通过ADO(接下来的文章将会细说这个)来重写。VB.NET 2003比早期的VB.NET在代码转换方面做得更好。所以这就意味着如果你要转换代码到.NET,可以跳过2002而直接转换成2003版本的。因此,我将不会去做转换到2002这样的事情,但是如果你非要这么做,我在这里所告诉你的东西同样适用(于2002)。

升级后,新的。NET项目将存储在其自己的文件夹,同时原来的VB6项目文件不作改变,所以只要你喜欢可以做尽可能多的试运行,还可以使用升级向导在最终转换好之前帮助问题的纠正。升级报告是一个列出文件及其每一个错误文件html文件

原文链接:https://www.f2er.com/vb/259089.html

猜你在找的VB相关文章