s = s & “Drive information for available drives:” & sCrLf
Dim drives As Object, drive As CDrive
If chkOld Then
Set drives = New CDrivesO
Else
Set drives = New CDrives
End If
For Each drive In drives
With drive
s = s & “Drive “ & .Root & “ [“ & .Label & “:” & _
.Serial & “] (“ & .KindStr & “) has “ & _
Format$(.FreeBytes, sBFormat) & “ free from “ & _
Format$(.TotalBytes, sBFormat) & sCrLf
End With
Next
Private drives As New Collection
Private Sub Class_Initialize()
Refresh
End Sub
‘ Argument handy for refreshing local and/or remote, but not floppies
Public Sub Refresh(Optional iFirst As Integer = 1)
Dim i As Integer, af As Long, sRoot As String
Dim drive As CDrive
‘ Remove old ones
Do While drives.Count > iFirst
drives.Remove iFirst
Loop
‘ Insert new
af = GetLogicalDrives()
For i = iFirst To 26
If RShiftDWord(af, i - 1) And 1 Then
Set drive = New CDrive
drive.Root = i
drives.Add drive, drive.Root
End If
Next
End Sub
Public Property Get Count() As Integer
Count = drives.Count
End Property
‘ Default property
Public Property Get Item(v As Variant) As CDrive
‘ Return default (Nothing) if error
On Error Resume Next
Set Item = drives(v)
End Property
Embedding a Collection object in the class and passing its members through with similar external members is another example of delegation. In a classic object-oriented language, you would use inheritance for the same purpose. Specifically, you would derive the CDrivesO class from the Collection class. You wouldn’t have to write any code to get the Count and Item properties. You would have to write code for any additional members you wanted (such as Refresh), and you’d disable or enhance any members you wanted to eliminate or change (such as Add and Remove).
At first glance, you might think it impossible to implement the Add and Remove properties. If you could add a new drive to your system just by calling the Add method, you’d never run out of disk space, but even the Plug and Play standard can’t promise that. On the other hand, it’s easy enough to use Add to connect to a network drive and Remove to disconnect one. Check out the WNetAddConnection2 and WNetCancelConnection2 API functions. I’ll leave it to you to enhance the CDrives collection to make it fully network-aware.