如果我使用 `CreateObject()` 设置一个变量,我是否需要在使用后将其设置为 `Nothing` 来清理它? [英] If I set a variable using `CreateObject()`, do I need to clean it up by setting it to `Nothing` after use?

查看:26
本文介绍了如果我使用 `CreateObject()` 设置一个变量,我是否需要在使用后将其设置为 `Nothing` 来清理它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用CreateObject()设置一个变量,我是否需要在使用后将其设置为Nothing来清理它?

If I set a variable using CreateObject(), do I need to clean it up by setting it to Nothing after use?

Dim foo
Set foo = CreateObject("SomeAssembly")
foo Bar
Set foo = Nothing

我刚刚找到了这篇文章埃里克·利珀特:

脚本引擎会在超出范围时自动清除这些变量,因此在超出范围之前清除它们的语句似乎毫无意义.

The script engine will automatically clear those variables when they go out of scope, so clearing them the statement before they go out of scope seems to be pointless.

推荐答案

如果我使用 CreateObject() 设置变量,是否需要在使用后将其设置为 Nothing 来清理它?

If I set a variable using CreateObject(), do I need to clean it up by setting it to Nothing after use?

通常情况下您不这样做,但您这样做已成为 VB 社区的传说.如果你是一个迷信的人,当你用完变量时,通过将变量设置为 Nothing 来抵御邪恶的眼睛并不会伤害,但它也很少有帮助.

Typically you do not, but it has become lore in the VB community that you do. If you are a supersitious person, it doesn't hurt to ward off the evil eye by setting variables to Nothing when you are done with them, but it rarely helps anything either.

您确实需要将变量设置为 Nothing 的极少数情况是:

The rare cases where you do need to set a variable to Nothing are those cases where:

  • 您创建了垃圾收集器无法清理的循环引用
  • 对象很昂贵,但变量是长期存在的,因此您希望尽早清理它
  • 对象实现需要必须维护的特定关闭顺序
  • 等等.

如果我不是其中一种情况,我不会将变量设置为 Nothing.我从来没有遇到过这个问题.

If I'm not in one of those cases, I don't set variables to Nothing. I've never had a problem with that.

这篇关于如果我使用 `CreateObject()` 设置一个变量,我是否需要在使用后将其设置为 `Nothing` 来清理它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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