Implementing CDrive
Now that you know exactly what you want, you can start to create the CDrive class. You can declare a class to be either private or public. I provide a public version in the VBCore DLL project. (VBCore is discussed in more detail later.) CDrive can just as easily be private. I provide a separate, private copy of the CDrive class in a file called PDRIVE.CLS. If you want to use the private version, put PDRIVE.CLS into your project rather than DRIVE.CLS.
Unfortunately, you can’t use the same class file for a private class in one project and a public class in another. I provide separate but otherwise identical private copies of all my public classes.
The private module names are the same as the public names with P_ as a prefix letter. They can be created by using the Save As
command and then changing the Instancing property to Private.
The Global Wizard described in Chapter 5 can translate private classes into public classes and vice versa automatically. This is an annoyance for maintenance, but you have to live with some similar scheme if you want to use the same class in different contexts.
There are two ways to define a property. The first way looks like a variable declaration:
Public Root As String
But if you define the Root property this way, you lose all control of it. The user can set or read it at any time without your knowledge. In CDrive, you need to do a lot of work whenever anyone sets the Root property. First you must confirm that the user has passed an actual root path. Next you calculate and store permanent data about the drive as soon as it is initialized. Then, when the user asks for the information later, you return the pre-calculated values.
The mechanism that makes this possible is the property procedure. Property Get, Property Let, and Property Set enable you to write procedures that look like public variables.
There are generally two strategies for defining properties, and CDrive follows them both. For properties that don’t change dynamically, it does all the initialization and calculation up front. In other words, the Property Let procedures do all the work and the Property Get procedures simply return pre-calculated internal data. But some data about a drive might change from moment to moment (for example, the number of free bytes). You want to recalculate these dynamic properties each time their Property Get is called. In other words, the Property Get procedure does all the real calculation and validation, while
the Property Let does the least work it can get away with.