Novice iLogic/VBA Discovery: Defining Classes - Dim vs. Set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
OK, here's another little tidbit that makes all the difference in the world for me to understand what the heck is going on in these VBA/iLogic programs. This is hidden info that nobody talks about (or KNOWS!), and everybody seems to ASSume that you DO know it. But UNLESS YOU STUDY VBA you will NEVER know this! Here it is taken from Mueller's book, VBA for Dummies, pages 183-4:
Defining Classes
You might want to think of a class as a substitute for a Function or a Sub, but classes are separate. A Function or Sub always describes a procedure — a list of steps. A class describes a thing. You can visualize a file because it’s a
thing. That’s why VBA uses classes to describe the file system and uses objects to work with individual file system elements, such as a drive. Although you might read that objects are substitutes for procedures, the two kinds of programming have definite places in your programmer’s toolbox. Make sure that you work with both as needed.
To build an object, you tell VBA to instantiate (create an instance of) the object. All the code required to build the object appears in the class. As a VBA user, you create the class and not the object. Here’s a simple example of
the two-step process used to instantiate an object:
‘ Create a reference to the file system. Dim MyFileSystem As FileSystemObject ‘ Create a reference for the target drive. Dim MyDrive As Drive ‘ Fill these two objects with data so they show the ‘ available space on drive C. Set MyFileSystem = New FileSystemObject Set MyDrive = MyFileSystem.GetDrive(“C”)
VBA creates the object, MyFileSystem, based on the blueprint provided by the FileSystemObject class. Likewise, VBA creates the object, MyDrive, based on the Drive class.
Telling VBA that you want to create these two objects by using the Dim statement is not the same as instantiating them. The Set statement instantiates the object. You Set an object equal to the blueprint contained within a class.
You can instantiate objects by using a number of techniques — the previous example shows two of them. In the first case, MyFileSystem is instantiated by using the New keyword and the name of the class, FileSystemObject.
In the second case, MyDrive is instantiated based on an existing object contained within the MyFileSystem object. The GetDrive method tells VBA which Drive object to use within the MyFileSystem object.
My way of saying it is: DIM DECLARES A VARIABLE, SET CREATES AN OBJECT.
The VARIABLE is a DATA CONTAINER, the OBJECT is the PROCESS that works on that DATA.
BIG difference between the two.
So now, when you see DIM and SET in VBA/iLogic macros, you know what they're doing, instead of just pretending to know, while using someone else's code to hack your way through your project AND HOPE IT WORKS!
Cheers
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator