I know set in VBScript is used for assigning the object reference to the variable. I would like to only understand why its neccessary:
Set fso = CreateObject("Scripting.FileSystemObject")
what about:
dim fso
fso = CreateObject("Scripting.FileSystemObject") //开发者_JAVA百科would not it create the object directly and assign to the variable?
Thanks
I think its just "because". The language is defined like this. You need it in case of CreateObject and new of a Class.
So its the difference between a normal variable and an object.
The same reason why IsNothing, IsNull, ... exists.
they are different. If you use Dim
to assign a variable, that's just it, a variable. But if you use set , you are actually "initializing" an object reference to a variable so that you can then call the object's "methods" eg.
Set objFS = CreateObject("Scripting.FileSystemObject")
Because now objFS
is a reference, you can do things like
Create a folder using objFS.CreateFolder
, or delete a folder: objFS.DeleteFolder
. Check if a file exists using objFS.FileExists
or get a file's extension using objFS.GetExtensionName
, among other things.
the concept is much like instantiating a class and using its methods in languages like Java/Python etc.
精彩评论