问题描述
要避免谁不喜欢的一般性问题的版主,这是一个Visio VBA之一,但我不希望包括在标题,因为它是一个有点小众,我想答案可能是通用的:-)
To avoid the moderators who don't like general questions, this is a Visio VBA one but I didn't want to include that in the title as it's a bit niche, and I guess the answer might be generic :-)
我的code具有以下变量:
My code has the following variables:
Public gappVisio As Visio.Application
Public gdocFile As Visio.Document
Public gpagDiagram As Visio.Page
对于那些unfamilar使用Visio,您创建一个应用程序对象,打开文档,然后设置一个引用页的文件中,你可以做一些实际的绘图。
For those unfamilar with Visio, you create an application object, open the document, then set a reference to a page in the document where you can actually do some drawing.
所有的VAR是全球性的,但实际上gdocFile只在我的初始化程序中使用。所以我的问题是,是否需要gdocFile作为全球性的,或者我可以只让地方?我想我很担心,如果是本地,当它走出范围可能整理文档对象,但我仍然需要文档的页面?
All vars are global, but actually gdocFile is only used in my initialisation routine.So my question is, do I need gdocFile as global, or can I just make it local?I suppose I was worried that if it was local when it went out of scope it might tidy up the Document object, but I still need the page of the document?
这是否有道理?
推荐答案
不要让一个变量或对象的全球除非你绝对有必要,这是几乎从来没有。传递对象引用作为参数传递给那些需要这些程序 - 并且只向那些。任何你从对象需要之前,用完范围,就像你说的,应该传递给调用的程序,函数返回值(或者更不明显稍欠preferably,子ByRef的参数值)。
Don't make a variable or object global unless you absolutely have to, which is almost never. Pass object references as parameters to those procedures that need them -- and only to those. Anything you need from the object before it "runs out of scope", as you say, should be passed to the calling procedure as Function return value (or, more obscurely hence less preferably, Sub ByRef parameter value).
当你说一个对象超出范围,它实际上是引用的那个对象是超出范围。该对象仍然存在不改变在内存中。
When you say an object is out of scope, it's actually the reference to that object that is out of scope. The object still exists unaltered in memory.
通常,全球不好,导致难以维持code,但例外可能是像普遍常量,例如
Generally, global is bad and leads to difficult-to-maintain code, but exceptions could be things like universal constants, e.g.
Public Const PI As Double = 3.14159265358979
它的优良有全球。
It's fine to have that as global.
这篇关于哪些对象进行全局,这使当地的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!