我有几个使用 FileSystemObject 的过程。我觉得挺方便的。
问题:将现有的 FileSystemObject 实例从“主”过程传递给这些其他过程作为参数,而不是让每个过程创建自己的 FileSystemObject 实例是否明智?
示例:以任何方式执行此操作是否更好:
Sub MainSub()
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Call OtherSub(FSO, myargs)
' call other subs and functions that use FileSystemObject
End Sub
Sub OtherSub(FSO, myargs)
' Do stuff with FSO
' call other subs and functions that use FileSystemObject
End Sub
我见过至少一个程序员这样做,而不是以下,这是我通常做的:
Sub MainSub()
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Call OtherSub(myargs)
' call other subs and functions that use FileSystemObject
End Sub
Sub OtherSub(myargs)
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Call OtherSub(myargs)
' Do stuff with FSO
' call other subs and functions that use FileSystemObject
End Sub
我可以看到执行前者的想法,因为这可能会减少与拥有多个 FileSystemObject 实例相关的开销。但是每次都必须将 FSO 作为参数传递似乎非常麻烦。说真的,开销真的那么大吗?