Implementing the CompressedFolder Class - Wrapping up
(Page 4 of 4 )
Private Sub Class_Initialize
Set objShell = CreateObject("Shell.Application")
Set objFso = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate
Set objShell = Nothing
Set objFso = Nothing
End Sub
The last two support methods are actually event handlers for the class itself. Thus, when a class is created or destroyed, events are fired. By capturing these events, a programmer has a place to execute code when a class is created or destroyed. For illustration purposes I’ve used these events as a common place to create and release the global objects used by my class.
Implementing the class in your scripts is quite simple. You’ll need to copy and paste the class into your script, and then instantiate it with the New keyword. Here’s what the class would look like in action to create a compressed folder and add a file to it.
Set objZipFolder = New CompressedFolder
objZipFolder.Create"C:archive.zip"
objZipFolder.Add "C:myfile.txt", vbTrue
WScript.Echo "You have successfully added a file to " & objZipFolder.Filename
Set objZipFolder = Nothing
Now you’ve seen how to create the class, and hopefully you also have a better understanding of the code and concepts that went into its development. There is no right or wrong way to code a particular class. Experience will teach you the most effective practices for accomplishing a task. I hope that you’ve gained a little insight into my own coding practices, and that you can take some of what I"ve shown here to help you create your own. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |