是“作为字典"和"As Scripting.Dictionary"等效(用于VBA早期绑定)? [英] Are "As Dictionary" and "As Scripting.Dictionary" equivalent (for VBA early binding)?

查看:151
本文介绍了是“作为字典"和"As Scripting.Dictionary"等效(用于VBA早期绑定)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用早期绑定和Microsoft脚本运行时库创建字典对象时,看起来同一事物有两个等效的类型名称:

When creating a dictionary object using early binding and the Microsoft Scripting Runtime Library, it looks like there are 2 equivalent type names for the same thing:

Dim dict as Dictionary
Set dict = New Dictionary

Dim dict as Scripting.Dictionary
Set dict = new Scripting.Dictionary

似乎做同样的事情(至少到目前为止).

appear to do the same thing (at least so far).

我看到该资源使用的As Scripting.Dictionary语法,并且我看到了语法(非常好)使用了 ,但是我没有在任何地方都看不到比较.而且有关字典的MSDN文档是文字笑话,或与以下内容没有明确关系VBA .

I see the As Scripting.Dictionary syntax used by this resource, and I see the As Dictionary syntax used by this (really great) resource, but I haven't seen a comparison anywhere. And the MSDN documentation on dictionaries is either a literal joke, or not explicitly related to VBA.

我不明白为什么我应该做更多的打字,只是如果我可以使用As Dictionary来使我的函数声明更加拥挤,但是我了解到VBA中的某些东西看起来像是等效的,实际上可以细微但明显的差异(例如Application.InputBoxInputBox).

I don't see why I should be doing extra typing just to make my function declarations more crowded if I can just use As Dictionary, but I have learned that some things in VBA that look like they are equivalent can actually have subtle but significant differences (Application.InputBox vs. InputBox for example).

这两个之间是否真的没有区别,还是我应该意识到一些细微的区别?

Is there is really no difference between these, or are there subtle differences that I should be aware of?

推荐答案

我不明白为什么我应该做额外的输入只是为了使我的函数声明更加拥挤[...]

它被称为完全限定类型.

执行此操作时:

Dim foo As Range

引用对话框中,以优先级顺序确定的特定顺序解析类型Range:

The type Range gets resolved in a very specific order, determined by the priority order, in the references dialog:

如果公开Range类型的最顶层类型库是Excel类型库,则Range代表Excel.Range.

If the top-most type library that exposes a Range type is the Excel type library, then Range stands for Excel.Range.

如果您想成为Word.Range,则说明您有一个错误,并且完全限定声明的类型可以避免这些错误.

If you meant that to be Word.Range, then you have a bug, and fully-qualifying your declared types avoids these bugs.

Scripting库公开了一个Dictionary类.因此,当您引用Scripting类型库并执行以下操作时:

The Scripting library exposes a Dictionary class. So when you reference the Scripting type library and do this:

Dim foo As Dictionary

您拥有所有权利,希望该类型解析为Scripting.Dictionary.但是,如果您添加一个类模块并将其命名为Dictionary,突然之间所有这些不合格的声明现在都在引用此自定义类,因为在查看外部引用之前,VBA类型解析将始终对当前项目中定义的类型进行优先级排序.

You have all rights to expect that type to resolve to Scripting.Dictionary. But then if you add a class module and name it Dictionary, suddenly all these unqualified declarations are now referring to this custom class, because VBA type resolution will always prioritize types defined in the current project before looking at external references.

当然,您可以将其称为额外打字".只要知道这两种方法都有优点和缺点即可.

Sure you can call it "extra typing". Just know that there are pros and cons to both approaches.

  • 完全合格的类型在引用公开同名类的类型库时避免了歧义:增强了可读性.
  • 通过
  • Not 限定类型,可以更轻松地将一个类替换为另一个类,例如将Scripting.Dictionary替换为在Mac上可用的便携式Dictionary实现.
  • Fully-qualifying types avoids ambiguity when referencing type libraries that expose same-name classes: it enhances readability.
  • Not qualifying types makes it easier to swap a class for another, e.g. replacing Scripting.Dictionary with a portable Dictionary implementation that works on a Mac.

经验法则,编程与编写代码无关.这是关于阅读代码的.编写易于阅读的代码,而不是快速编写的代码.

Rule of thumb, programming isn't about writing code. It's about reading code. Write code that's easy to read, not code that's quick to write.

这篇关于是“作为字典"和"As Scripting.Dictionary"等效(用于VBA早期绑定)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆