Classes are available to VBScript 5.0 and up (installed with IE5).
Classes allow a user to group similar methods together as well as properties that describe the class. They allow us to increase the modularity of our code as well as let us write less lines of code in script that use it.
For example, imagine you have a script that does work and then logs the results of your work. You might have methods that allow you to write a header, a line of data, footer, and then close the file. To actually write to the file, you would need to use the FileSystemObject, OpenTextFile, and the Close methods. All of these methods and object are associated with each other and need each other to work together. Instead of re-creating the code each time, you can create a class that encompasses the basic methods for file manipulation and the associated objects.
To declare a class, you will need to use the Class statement.
Class LogKeeper
End Class
Now that you have your class declared, we will add class-wide variables that are meaningful to our class.
Class LogKeeper
Private m_OpenFile 'Stores an TextSteamObject we will be using to write the data to
Private m_LogPath 'Stores the path we will write the log to
Private m_IOMode 'Stores how the OpenTextFile method will treat the file stream
End Class
You might have noticed the “Private” in front of the variables. This is an access modifier keyword. It allows you to specify the scope of the method or variable. In this case, we will be working with the Public and Private modifiers. Private means the method/variable is only available to the objects within the class and the Public modifier means the method/variable is available to the objects outside of the class.
Sometimes you will have a class-wide variable that you need to access, but you don’t want to always declare it or pass it to the methods. In this case, you would make it a property of a class. In this example, we will make a property for the m_LogPath variable so we can set and get the value of the variable.
Class LogKeeper
Private m_OpenFile
Private m_LogPath
Private m_IOMode
'The "Get" property allows an external object to get the value of the m_LogPath variable
Public Property Get LogPath
LogPath = m_LogPath
End Property
'The "Let" property allows an external object to set the value of the m_LogPath variable
Public Property Let LogPath(value)
m_LogPath = value
End Property
Public Property Get IOMode
IOMode = m_IOMode
End Property
'We check to see if the value provided is a acceptable value
'If it isn't we default to IOMode of appending to the text file
Public Property Let IOMode(value)
If value <> 2 And value <> 8 Then
m_IOMode = 8
Else
m_IOMode = value
End If
End Property
End Class
By adding a property to the class, it allows us to retrieve and set the value of the private variable. This lets us set the value once and allow the class to make the necessary calls to it instead of passing it multiple times or having it clutter the code of the main work script.
<message edited by Wakawaka on Friday, March 04, 2011 4:47 PM>