tags:

views:

30

answers:

2

Hi, 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") //would not it create the object directly and assign to the variable?

Thanks

A: 

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.

Totonga
A: 

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.

ghostdog74
Um, if the FSO is actual object, I should be able to do the same.BTW, why did you write "objFS" instead of fso? I thought the methods will be fso.CreateFolder etc.< Thanks
Snake
objFS, FSO, it doesn't matter. as long as its a meaningful name. and yes, i have fixed the name.
ghostdog74
Well, I understand the concept, just do not understand using of "set". In other languages you can assign reference just by =.Class a=new class A();RefA=a;
Snake