Understanding the CompressedFolder Class - More on Properties
(Page 3 of 4 )
Of my first three properties, the FullName (path and filename to the compressed folder) was the easiest to do first. This needed to be a user-provided value. The class user would need to indicate what compressed folder to create or open. Very simply, this property would assign or retrieve an internal variable that contained the user’s supplied path and filename.
The Filename and Path properties would be easy to code once the value of the FullName property had been assigned, since they are just pieces of that same string. A little clever use of some string functions would parse out the pieces that I needed. Remember, your code blocks don’t need to be fancy, just functional. This simplest and most direct approach is usually the easiest and most efficient.
Public Property Get Filename
Filename = Right(FullName, Len(FullName) - InStrRev(FullName, ""))
End Property
Public Property Get Path
Path = Left(FullName, InStrRev(FullName, ""))
End Property
For the Filename property, I simply found the last forward slash in the string returned by the FullName property and grabbed everything to the right of it. According to standard Windows file naming conventions, this would be the folder or file name.
Another revision might include an update that would prevent an extra forward slash at the end of the full path. I chose to omit this for simplicity, and because compressed folders already contain a .zip extension indicating a file path rather than a folder path.
The Path property should return the path to the compressed folder. Here again I can look for the last forward slash in the string returned by the FullName property, this time grabbing everything to the left of it instead. Essentially, this is the FullName value minus the Filename portion—the exact opposite of the last property.
You might also notice that none of these properties allow public assignment. Filename and Path are read-only, since they are dependent upon the value of the FullName property, and although the FullName property requires a user-submitted value, I’ve elected to not allow users to set this value directly. It didn’t seem necessary to have my users supply a property and then call a method, when I could just as easily do everything in a single step without seeming complicated.
Instead of assigning the FullName property directly, the property value is set by the Open method. This also provided a convenient opportunity for me to do some error handling without having too much code inside of my property statements.
In my opinion, Property blocks are for assigning and retrieving property values. Any extraneous code belongs in a method of its own. Code appears neater that way, and it provides a more modularized approach.
Next: Examining the Exposed Methods >>
More Code Examples Articles
More By Nilpo