Initializing CDrive data
First let’s look at the private variables that CDrive will have to fill:
Private sRoot As String
Private edtType As EDriveType
Private iTotalClusters As Long
Private iFreeClusters As Long
Private iSectors As Long
Private iBytes As Long
Private sLabel As String
Private iSerial As Long
Private fDriveMissing As Boolean
The key property for users is Root because it is the default property. Users will normally set it before they set anything else. The Property Let looks like this:
Public Property Let Root(vRootA As Variant)
‘ Some properties won’t work for \\server\share\ drives on Windows 95
sRoot = UCase(vRootA) ‘ Convert to string
InitAll
End Property
Clearly, the key procedure from the programmer’s standpoint is InitAll. This function gets called when a CDrive object is created (in Class_Initialize) and each time the user sets the Root property. InitAll looks like this:
Private Sub InitAll()
sLabel = sEmpty: iSerial = 0
iSectors = 0: iBytes = 0: iFreeClusters = 0: iTotalClusters = 0
fDriveMissing = False
‘ Empty means get current drive
If sRoot = sEmpty Then sRoot = Left$(CurDir$, 3)
‘ Get drive type ordinal
edtType = GetDriveType(sRoot)
‘ If invalid root string, try it with terminating backslash
If edtType = edtNoRoot Then edtType = GetDriveType(sRoot & “\”)
Select Case edtType
Case edtUnknown, edtNoRoot
Dim iDrive As String
iDrive = Val(sRoot)
If iDrive >= 1 And iDrive <= 26 Then
sRoot = Chr$(iDrive + Asc(“A”) - 1) & “:\”
Else
sRoot = sEmpty
End If
‘ Start over
InitAll
Case edtRemovable, edtFixed, edtRemote, edtCDROM, edtRAMDisk
‘ If you got here, drive is valid, but root might not be
If Right$(sRoot, 1) <> “\” Then sRoot = sRoot & “\”
GetLabelSerial
Case Else ‘ Shouldn’t happen
BugAssert True
End Select
End Sub
That might look like a lot of work, but essentially this procedure is the CDrive class. It calculates almost everything necessary to allow the Property Get procedures simply to read internal variables.
Notice the recursive call to InitAll when the drive type is unknown or invalid. This happens if the user sets the Root property to a bogus value, such as an empty string. In other words, you have an error. Or have you?
There are many ways to handle errors in classes, but one choice is to refuse them. If you accept any input (including no input), there can’t be any user error. If something goes wrong, it’s the programmer’s fault. The CDrive class attempts to follow this strategy. For example, if you enter the string invalid drive as the Root property, CDrive will first interpret it as a root drive string. If that fails (and it will), CDrive will interpret it as a drive number. If that fails (and it will), CDrive will interpret it as the current drive, and there’s always a current drive. We’ll look at other error strategies later.