VBScript
VBScript
Session 17
Session 17
?What we learn last session
Subjects for session 17
Subjects for session 17
Class object.
Class object.
Class statement.
Class statement.
Property Let.
Property Let.
Property Get.
Property Get.
Property Set.
Property Set.
Initialize event.
Initialize event.
Terminate event.
Terminate event.
Inline classes.
Inline classes.
Classes in VBScript
Classes in VBScript
Class Object
Class Object
The object created using the
The object created using the Class
Class statement.
statement.
Provides access to the events of the class.
Provides access to the events of the class.
You cannot explicitly declare a variable to be of
You cannot explicitly declare a variable to be of
type Class.
type Class.
In the VBScript context, the term "class object"
In the VBScript context, the term "class object"
refers to any object defined using the VBScript
refers to any object defined using the VBScript
Class
Class statement.
statement.
Dim
Dim XX Set
Classes in VBScript
Classes in VBScript
Class Statement
Class Statement
Declares the name of a class, as
Declares the name of a class, as
well as a definition of the v
well as a definition of the variables,
ariables,
properties, and methods that
properties, and methods that
comprise the class.
comprise the class.
Syntax
Syntax
Class
Class namename statements statements End Class End Class
Classes in VBScript
Classes in VBScript
Class Statement
Class Statement
Within aWithin a ClassClass block, members are declared as eitherblock, members are declared as either PrivatePrivate oror
Public
Public using the using the appropriate declaratioappropriate declaration statements.n statements.
Anything declared asAnything declared as PrivatePrivate is visible only within theis visible only within the ClassClass block.block.
Anything declared asAnything declared as PublicPublic is visible within theis visible within the ClassClass block, as wellblock, as well
as by code outside the
as by code outside the ClassClass block.block.
Anything not explicitly declared as eitherAnything not explicitly declared as either PrivatePrivate oror PublicPublic isis PublicPublic
by default. by default.
Procedures (eitherProcedures (either SubSub oror FunctionFunction) declared) declared PublicPublic within the classwithin the class
block become methods of the class. block become methods of the class.
PublicPublic variables serve as properties of the class, variables serve as properties of the class, as do propertiesas do properties
explicitly declared using
explicitly declared using PropertyProperty GetGet,, PropertyProperty LetLet, and, and PropertyProperty
Set
Set..
Default properties and methods for the class are Default properties and methods for the class are specified in theirspecified in their
declarations using the
Classes in VBScript
Classes in VBScript
Get Property Statement
Get Property Statement
Declares, in a
Declares, in a Class
Class block, the name,
block, the name,
arguments, and code that form the body
arguments, and code that form the body
of a
of a Property
Property procedure that gets
procedure that gets
(returns) the value of a property.
(returns) the value of a property.
Syntax
Syntax
[[PublicPublic [[DefaultDefault] |] | PrivatePrivate]] Property GetProperty Get namename [([(arglist arglist ))]] [[statementsstatements]]
[[
[[SetSet]] namename == expressionexpression]] [[Exit PropertyExit Property]]
Classes in VBScript
Classes in VBScript
Get Property Statement
Get Property Statement
If not explicitly specified using eitherIf not explicitly specified using either PublicPublic oror PrivatePrivate,,
Property
Property GetGet procedures are public by default, that is, theyprocedures are public by default, that is, they are visible to all other procedures in your
are visible to all other procedures in your script.script.
The value of local variables in aThe value of local variables in a Property GetProperty Get procedure isprocedure is
not preserved between calls to the procedure. not preserved between calls to the procedure.
You can't define aYou can't define a Property GetProperty Get procedure inside any otherprocedure inside any other
procedure. procedure.
TheThe Exit PropertyExit Property statement causes an immediate exit fromstatement causes an immediate exit from
a
a PropertyProperty GetGet procedure.procedure.
PropertyProperty GetGet procedure is a separate procedure that procedure is a separate procedure that cancan
take arguments, perform a series of statements, and change take arguments, perform a series of statements, and change the value of its arguments.
Classes in VBScript
Classes in VBScript
Let Property Statement
Let Property Statement
Declares, in a
Declares, in a Class
Class block, the name,
block, the name,
arguments, and code that form the body
arguments, and code that form the body
of a
of a Property
Property procedure that assigns
procedure that assigns
(sets) the value of a property.
(sets) the value of a property.
Syntax
Syntax
[[PublicPublic [[DefaultDefault] |] | PrivatePrivate]] Property LetProperty Let namename (([[arglist arglist ,],] valuevalue)) [[statementsstatements]]
[[
[[SetSet]] namename == expressionexpression]] [[Exit PropertyExit Property]]
[[statementsstatements]] End Property End Property
Classes in VBScript
Classes in VBScript
Let Property Statement
Let Property Statement
If not explicitly specified using eitherIf not explicitly specified using either PublicPublic oror
Private
Private,, Property LetProperty Let procedures are public byprocedures are public by default, that is, they
default, that is, they are visible to all other proceduresare visible to all other procedures in your script.
in your script.
The value of local variables in aThe value of local variables in a Property LetProperty Let
procedure is not preserved between calls to the procedure is not preserved between calls to the procedure.
procedure.
You can't define aYou can't define a Property LetProperty Let procedure inside anyprocedure inside any
other procedure. other procedure.
TheThe Exit PropertyExit Property statement causes an immediatestatement causes an immediate
exit from a
exit from a Property LetProperty Let procedure.procedure.
EveryEvery Property LetProperty Let statement must define at leaststatement must define at least
one argument for the procedure it defines. one argument for the procedure it defines.
Classes in VBScript
Classes in VBScript
Set Property Statement
Set Property Statement
Declares, in a
Declares, in a Class
Class block, the name,
block, the name,
arguments, and code that form the body
arguments, and code that form the body
of a
of a Property
Property procedure that sets a
procedure that sets a
reference to an object.
reference to an object.
Syntax
Syntax
[[PublicPublic [[DefaultDefault] |] | PrivatePrivate]] Property SetProperty Set namename (([[arglist arglist ,],] referencereference)) [[statementsstatements]]
[[
[[SetSet]] namename == expressionexpression]] [[Exit PropertyExit Property]]
[[statementsstatements]] End Property End Property
Classes in VBScript
Classes in VBScript
Set Property Statement
Set Property Statement
If not explicitly specified using eitherIf not explicitly specified using either PublicPublic oror
Private
Private,, PropertyProperty SetSet procedures are public byprocedures are public by default, that is, they
default, that is, they are visible to all other proceduresare visible to all other procedures in your script.
in your script.
The value of local variables in aThe value of local variables in a PropertyProperty SetSet
procedure is not preserved between calls to the procedure is not preserved between calls to the procedure.
procedure.
You can't define aYou can't define a PropertyProperty SetSet procedure inside anyprocedure inside any
other other
TheThe ExitExit PropertyProperty statement causes an immediatestatement causes an immediate
exit from a
exit from a PropertyProperty SetSet procedure.procedure.
EveryEvery PropertyProperty SetSet statement must define at leaststatement must define at least
one argument for the procedure it defines. one argument for the procedure it defines.
Classes in VBScript
Classes in VBScript
Initialize Event
Initialize Event
Occurs when an instance of the
Occurs when an instance of the
associated class is created.
associated class is created.
Syntax
Syntax
Private Sub
Private Sub
Class_Initialize()
Class_Initialize()
statements
statements
End Sub
End Sub
Set
Set X =X = NewNew TestClassTestClass ' Create an instance of TestClass.' Create an instance of TestClass.
Set
Classes in VBScript
Classes in VBScript
Terminate Event
Terminate Event
Occurs when an instance of the
Occurs when an instance of the
associated class is terminated.
associated class is terminated.
Syntax
Syntax
Private Sub
Private Sub
Class_Terminate()
Class_Terminate()
statements
statements
End Sub
End Sub
Set
Set X =X = NewNew TestClassTestClass ' Create an instance of TestClass.' Create an instance of TestClass.
Set
Classes in VBScript
Classes in VBScript
Inline Classes
Inline Classes
Inline class is a class that you
Inline class is a class that you can
can
immediatelly use without registering
immediatelly use without registering
the class.
Classes in VBScript
Classes in VBScript
MyArray Class - Example
MyArray Class - Example
Inline class is a class that you
Inline class is a class that you can
can
immediatelly use without registering
immediatelly use without registering
the class.
the class.
VBScript\VBS\class.vbs
Creating a COM Class
Creating a COM Class
Step 1
Step 1
Creating a COM Class
Creating a COM Class
Step 2
Step 2
Creating a COM Class
Creating a COM Class
Step 3
Step 3
Remove the existing empty
Remove the existing empty
class
class
Creating a COM Class
Creating a COM Class
Step 4
Step 4
Creating a COM Class
Creating a COM Class
Step 5
Step 5
Creating a COM Class
Creating a COM Class
Step 6
Step 6
Add properties, methods, events and
Add properties, methods, events and
enums using the class builder.
enums using the class builder.
Creating a COM Class
Creating a COM Class
Step 7
Step 7
Make sure to visit us
Make sure to visit us
Tutorials
Tutorials
Articles
Articles
Proikects
Proikects