VB6:禁用变体

发布于 2024-12-28 06:03:32 字数 312 浏览 0 评论 0原文

我有一个大型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断肠人 2025-01-04 06:03:32

我曾经使用 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.

池予 2025-01-04 06:03:32

使用Option Explicit 装饰您的模块。

该短语应位于您创建的每个模块的顶部。这样做时,遇到未声明的变量时会导致编译器错误。

但是,Option Explicit 不会阻止无类型变量声明,例如

Dim i

变量 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 as

Dim i

The variable i will be declared as a variant, and no compiler error will be thrown even with Option Explicit defined.

桃扇骨 2025-01-04 06:03:32

我认为没有一种“万无一失”的方法来检测所有未定义的变量。但是,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.

何以畏孤独 2025-01-04 06:03:32

使用程序员的文本编辑器(我使用 UltraEdit)并在项目源目录中进行大量搜索。

从搜索 Variant 开始(显然),尽管您可能已经这样做了。

接下来使用正则表达式类型搜索以下内容:

 *Dim [a-zA-Z][a-zA-Z0-9_]*\p

应该得到 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

使用 PrivateGlobal 而不是 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:

 *Dim [a-zA-Z][a-zA-Z0-9_]*\p

That should get the Dim x scenario without the trailing As DataType.

Use *Dim [a-zA-Z][a-zA-Z0-9_]*,.* to find the Dim a, b, c As Integer type of scenarios.

Use *Dim .*, [a-zA-Z][a-zA-Z0-9_]*,.* for odd ball scenarios like Dim a As Integer, b, c As Long

Repeat the above seaches with Private and Global instead of Dim and that should get just about everything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文