Envision , Evolve, Unleash
VB Script Classes
• VB Script version 5 is having the ability to write Classes. • Object is an instance of class and it is an in-memory representation of a complex data and programming structure that can exist only while program running. • Class is a template for object; it is a block of code. • Component is nothing more than a packaging mechanism, a way of compiling one or more related classes into a binary file that can distributed to one or more computers. Ex:- .DLL, .OCX
10/22/08
2
Class Syntax Class classname Code End Class
10/22/08
3
• Properties When a script creates an object based on class, properties are the mechanisms through which data is stored and accessed. 1. Private Property 2. Public Property
10/22/08
4
• Property Let is a special kind of procedure that allows code outside of a class to place a value in a private property value. Class customer Private localvar Public Property Let customername(outsidevar) Localvar = outsidevar End Property End Class 10/22/08
5
• Property Get is inverse of a Property Let procedure. It is allows code outside of your class to read the value of a private property variable. Class customer Private localvar Public Property Let customername(outsidevar) Localvar = outsidevar End Property Public Property Get customername() customername = Localvar End Property End Class
10/22/08
6
• Property Set is very similar to Property Let procedure, but the Property Set procedure is used exclusively for object-based properties. When the property needs to store an object. Class customer Private localobj Public Property Set customername(outsideobj) localobj = outsideobj End Property End Class *Code outside of your class must use the Set Object.Property = Object syntax 10/22/08
7
• Making a Property Read-Only • -By providing only a Property Get procedure -By declaring the Property Get procedure as public and the Property Let procedure as Private • Making a Property Write-Only • -You can omit the Property Get procedure and provide only a Property Let procedure -You can declare the Property Let procedure with Public statement, and declare the Property Get procedure with Private statement 10/22/08
8
• Public Properties without Property Procedures Class customer Private localvar Public Property Let customername(outsidevar) Localvar = outsidevar End Property Public Property Get customername() customername = Localvar End Property End Class 10/22/08
9
• Above code is equal to Class customer Public localvar End Class
10/22/08
10
• Methods is a different name for functions and procedures • Class Events is a method that is called automatically. It calls when event fires.
10/22/08
11
• Class_Initialize Event fires when some code instantiates an object that is based on your class. Class customer Private localobj Public Sub Class_Initialize Set localobj = WScript.CreateObject(“Scripting.FileSystemObject”) End Sub End Class 10/22/08
12
•
Class_Terminate Event fires when an object that is based on your class is destroyed. Class customer Private localobj Public Sub Class_Initialize Set localobj = WScript.CreateObject(“Scripting.FileSystemObject”) End Sub Public Sub Class_Terminate Set localobj = Nothing End Sub End Class
10/22/08
13
• Class level Constants doesn’t support by VB script. We can use Read only property instead of the constants. • Class level Arrays has supporting in upgraded versions of VB Script
10/22/08
14
• No
Q&A 10/22/08
plz
15
The End
10/22/08
16