简体   繁体   中英

Option Infer ON

Does option infer not work in VBA?

I'm try to test out VB tutorial code on arrays in Microsoft Access.

Dim numbers = {{1, 2}, {3, 4}, {5, 6}}

This doesn't work without Option Infer ON, but you get compile error if you add it in.

Total noob question.

Option Infer指令是Visual Studio 2008中添加到VB.Net的功能。VBA编译器是完全不同的实现,从未为Option Infer更新,因此无法进行这项工作

Just to add to this post; though Option Infer is not present, in VBA you can still turn Option Explicit off which gives you a poor mans version. Be careful, however, as this isn't truly Option Infer . What you're doing is simply making VBA primarily use variants to store the values. The VBA engine then decides what the best subtype of variant is. For example:

Dim a, b
a = 2
b = 3.2
Debug.Print a * b

Will work quite happily. However, you can then go on to change the value later, like so...

Dim a, b
a = 2
b = 3.2
Debug.Print a * b
a = "Hello, World!"
Debug.Print a

Which works quite happily and produces the output:

 6.4
Hello, World!

Not the best practice.

Revision

Just to further reinforce the poor quality of this approach, I have had several experiences where the [original programmer's] code has happily crashed, bringing down the whole application, because variables were not properly defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM