VB6:禁用变体
我有一个大型 VB6 项目,其中许多变量没有显式定义的类型,因此它们自动默认为 Variant
类型。手动查找所有这些是一项艰巨的任务,那么有什么方法可以自动化执行此操作呢?在 VB.Net 中,可以使用“Option Strict”禁用所有自动使用变体,但 VB6 没有该选项。
现在,我向每个类添加了 DefByte AZ
,这使得默认类型为“Byte”而不是“Variant”。这让我可以在运行时捕获大量未定义的变量,只要它们被分配的值大于 255。但这仍然不是完全万无一失的。
有没有更可靠的方法来检测所有未定义的变量?
I have a large VB6 projects where a lot of variables don't have an explicitly defined type, so they automaticly default to Variant
type. Finding all those by hand is a massive task, so is there any way to automate this? In VB.Net it's possible to disable all automatic use of variants using 'Option Strict', but VB6 doesn't have that option.
Right now I added DefByte A-Z
to every class, which makes the default type 'Byte' instead of 'Variant'. This let me catch a lot of undefined variables at run-time, as soon as they are assigned a value larger than 255. But it's still not fully fool-proof.
Is there a more reliable way to detect all undefined variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我曾经使用 Aivosto 的项目分析器 来获取类似的内容。有一个演示版本可以让您很好地了解它的功能。
I used to use Aivosto's Project Analyzer to pick up things like this. There's a demo version which will give you a good idea what it can do.
使用
Option Explicit
装饰您的模块。该短语应位于您创建的每个模块的顶部。这样做时,遇到未声明的变量时会导致编译器错误。
但是,
Option Explicit
不会阻止无类型变量声明,例如变量
i
将被声明为变体,即使使用 <,也不会引发编译器错误code>Option Explicit 已定义。Decorate your modules with
Option Explicit
.This phrase should go at the top of each module you create. When done so, it will cause a compiler error when undeclared variables are encountered.
Option Explicit
will not, however, prevent type-less variable declarations, such asThe variable
i
will be declared as a variant, and no compiler error will be thrown even withOption Explicit
defined.我认为没有一种“万无一失”的方法来检测所有未定义的变量。但是,Option Explicit 语句将要求所有变量都在该语句出现的模块中声明,因此编译器将标记任何不是这种情况的实例。还有一个 IDE 选项会自动将此语句添加到任何新模块的开头。
I don't think there's a "foolproof" way to detect all undefined variables. However, the Option Explicit statement will require that all variables be declared in the module in which the statement appears, so the compiler will flag any instances where that is not the case. There is also an IDE option that automatically adds this statement to the start of any new module.
使用程序员的文本编辑器(我使用 UltraEdit)并在项目源目录中进行大量搜索。
从搜索
Variant
开始(显然),尽管您可能已经这样做了。接下来使用正则表达式类型搜索以下内容:
应该得到
Dim x
场景,而没有尾随As DataType
。使用
*Dim [a-zA-Z][a-zA-Z0-9_]*,.*
查找Dim a, b, c As Integer
类型场景。对于奇怪的情况,请使用
*Dim .*, [a-zA-Z][a-zA-Z0-9_]*,.*
,例如Dim a As Integer, b, c As Long
使用
Private
和Global
而不是Dim
重复上述搜索,这样就可以得到几乎所有内容。Use an programmer's text editor (I use UltraEdit) and do a mass search across you project source directories.
Start with searching for
Variant
(obviously), though you probably already did that.Next use a regular expression type search for something along the lines of:
That should get the
Dim x
scenario without the trailingAs DataType
.Use
*Dim [a-zA-Z][a-zA-Z0-9_]*,.*
to find theDim a, b, c As Integer
type of scenarios.Use
*Dim .*, [a-zA-Z][a-zA-Z0-9_]*,.*
for odd ball scenarios likeDim a As Integer, b, c As Long
Repeat the above seaches with
Private
andGlobal
instead ofDim
and that should get just about everything.