如何将引用变量设置为空 [英] How to set referenced variable to nothing

查看:95
本文介绍了如何将引用变量设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些变量设置为空。

I would like to set some variables to nothing.

我不想重复代码,所以我想将每个var添加到一个数组并使用相同的例程发布所有变量

I do not want to repeat code, so I would like to add each var to an array and use the same routine to release all vars

  Public Sub DesconectarVars()
        Dim Objetos As Object() = {ExcelWorksheet, ExcelWorkbook, ExcelApp}
        For Each Objeto In Objetos
            Marshal.ReleaseComObject(Objeto)    
            Objeto = Nothing
        Next Objeto
    End Sub

问题是,当我写"Objeto = nothing"时,Objeto设置为空,而不是引用的变量(我想设置ExcelApp = Nothing,而不是Objeto = Nothing)。

The problem is that when I write "Objeto = nothing", Objeto is set to nothing, and not the referenced variable (I want to set ExcelApp=Nothing, not Objeto=Nothing).

有什么办法吗?

推荐答案

问题在于它什么都不做。 

The problem is that it does nothing. 

VB从版本7开始就是一种OOP程序语言,所以你把它设置为空的地址就变成了空的。  ;

VB is since version 7 an OOP program language, so what you do with setting it to nothing the address becomes empty. 

因此,你有一个理由想要这样做,在目前的方法中,AFAIK没有任何意义。 

Therefore have you a reason you want to do it, in the current method it has AFAIK no sense. 

(1998年有一个错误我从VB6开始思考SP5然后成为神话,所有事情都必须设置为空,尽管SP6解决了这个问题但它永远不会消失。)

(In 1998 there was a bug I thought in SP5 from VB6 and then became the myth that everything had to be set to nothing, it never went away although SP6 solved the bug).

.Net意味着托管程序语言,所有东西释放内存是由.Net完成的。但是,有些人想自己做,而不是把它锁起来而不是释放它。 

.Net means managed program language, all things around releasing memory is done by .Net. However, some want to do it themselves and than lock it up instead of releasing it. 

因此,你认为什么不能去? (例如,是否存在内存不足消息并关闭计算机?)

Therefore, what does not go as it in your opinion has to go? (For instance comes there an out of memory message and shuts the computer down?)


这篇关于如何将引用变量设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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