Article Directory
- VBScript 1 in the class
- 1.1 objects, classes and components
- 1.2 Class statement
- 1.3 Definitions property
- 1.3.1 Private property variable
- 1.3.2 Property Let
- 1.3.3 Property Get
- 1.3.4 Property Set
- 1.3.5 Creating a read-only attribute
- 1.3.6 create a write-only property
- 1.4 method definitions
- 1.5 class event
- 1.6 Construction and use of classes VBScript
VBScript 1 in the class
1.1 objects, classes and components
对象
Is a complex data structure and program performance in memory, that only exists if the program runs对象与数组还是有些区别
The most important thing is that the object is not just to store complex data (in the form of property) as an array; the object and "behavior" (that is to say "it knows how to do that.") This method performance. Attribute can hold any type of data, and the method may be a process or function. The data and behavior together into objects, the code data and operation data so that the design process can be operated together类是对象的模板
. Only objects at run time only exists in memory, and the program is designed to structure classes at design time can be used directly.类是代码
, And the object is to use this code at runtime. If you are using objects at run time, you must first define a class at design time. Will create objects at run time based on the template provided by the class. For example, you can write a class named Customer. Once you save the class definition, you can then create any number of other code Customer object in memory.
1.2 Class statement
- Create a VBScript Class statement is the class keyword. With a
Function … End Function 或 Sub … End Sub
boundary setting process similar statement, Class and End Class statement also applies to the boundary of the set class. Plurality may be used in a scriptClass … End Class
block of code defines a plurality of classes (but not nested in VBScript class). - Typically, you must create custom code to the code class instance VBScript classes together. This appears to be a lot of restrictions (because an object is to create a class convenient portable and reusable code) but there are two other methods to achieve this purpose.
- In addition to the difference in whether the same script file, Visual Basic programmers can no obstacles to adapt to the use VBScript classes. In addition to both Visual Basic and VBScript differences in language, structure and related technologies like VBScript and Visual Basic are the same.
Here is the basic syntax of the Class.
Class MyClass
< rest of the class code will go here >
End Class
Of course, it should be replaced with the name of the class in which his own definition MyClass
. The name of the class and with the same scope INCLUDE indicator contains the name of the incoming class, the script should be unique. The class name can not be repeated VBScript reserved word (or such Function While)
1.3 Definitions property
In the script create objects based on class, attribute is a mechanism for storing and accessing data. By attributes, data can be stored in the object; data may be acquired from a subject through properties
1.3.1 Private property variable
- The best way to store the value of the property is private property variables.
- This is defined as a class-level variable scope (beginning of the class). This variable is private (that is to say can not access this variable from outside the class), storing property values. The code uses the class to interact with it through · Property Let, Set and Get · processes, but these processes only gateway to the private property variables only.
Private property can be defined as a variable:Class Customer
Private mstrName
< rest of the class code will go here >
End Class
1.3.2 Property Let
Property Let
Process is a special process for assignment to the private property variables outside the class. Property Let
VBScript process is similar to the sub-process has no return value. Here is the syntax.
Class Customer
Private mstrName
Public Property Let CustomerName(strName)
mstrName = strName
End Property
End Class
- There is no use
Sub 或 Function
statements to define this process, but the use ofProperty Let
.Property Let
Process must receive at least one parameter. Without this parameter can not achieveProperty Let
the purpose of the process, i.e. such that the outer code can store a value to the private property variables. - Here we must pay attention to the process of how the code attribute is passed to the process of
strName
saving the value of themstrName
private property variables. This process may not have any code, it will not pass over the values stored in a variable or object, but this is totally incompatible with theProperty Let
purpose of the process. - May be optionally added to the process code. Before value in some cases, you may want to pass over private property truly assign variables to them to do some checking. For example, if the length of the customer's name can not be more than 50 characters, you need to check strName parameters are more than 50 characters in length; if more than this length, use Err.Raise () method call to tell this error code.
- Finally, the process must attribute
End Property
the statement ending (likeFunction 过程必须以 End Function
the end ofSub 过程必须以 End Sub
the end of the same). If you want to jump out of property procedures, you can useExit Property
the statement (with aExit Function
jump Function, withExit Sub
out of the same Sub).
1.3.3 Property Get
Property Get
Process with the Property Let
process is reversed. Property Let
Private property is used to process variables outside the class code assignment, and Property Get
the process is allowed to read the value of the code external to the private property variables. Property Get
Function procedure similar process with VBScript, there is a return value. Here is its syntax.
Class Customer
Private mstrName
Public Property Let CustomerName(strName)
mstrName = strName
End Property
Public Property Get CustomerName()
CustomerName = mstrName
End Property
End Class
- And
VBScript
theFunction
process is similar toProperty Get
the process of code that calls it will return a value. This value is usually the value of private property variables. Note Property Get procedure name with the correspondingProperty Let
name of the procedure is the same.Property Let
The value stored in the process variable private attributes, andProperty Get
the process then it is read out. Property Get
Process does not require any parameters. VBScript allowed to join the argument, but if you want to do this, we must attribute this to the Property Let or Property Set procedure (if you use this) also add an additional parameter. Because theProperty Let/Set
parameters of the process than the corresponding parameter must be a Property Get procedure more. AProperty Let/Set
process of adding an extra parameter is difficult to see, and require the use of this type of code is more than one parameter in the form of very good Property Let process. If you feel that aProperty Get
process does require a parameter, it is best to add an additional property to achieve thisProperty Get
parameter function. If the Property Get procedure returns a pointer to the object variable reference, you may need toSet
return this value statement. E.g:
Class FileHelper
' Private FileSystemObject object
Private mobjFSO
Public Property Get FSO()
Set FSO = mobjFSO
End Property
End Class
1.3.4 Property Set
Property Set
Process andProperty Let
the process is very similar, butProperty Set
the process is for the object-based properties. When the need to store an object (instead of a number, date, boolean, or string of variable subtype) in properties, can provide aProperty Set
process of replacing aProperty Let
process. Here is a Property Set procedure syntax.
Class FileHelper
' Private FileSystemObject object
Private mobjFSO
Public Property Set FSO(objFSO)
Set mobjFSO = objFSO
End Property
End Class
In terms of functionality, Property Let
and Property Set
functional process is the same. Property Set procedure but there are two differences:
- Clearly stated this property is a property of an object-based (meaning any code that makes the technology more explicit than other same functionality as technology is more favored).
- External class code must be
Set Object.Property = Object
written to this attribute (as this is a typical way to achieve this function, so this is the advantage).
For example, the following code creates an object class in accordance with previously described.
Dim objFileHelper
Dim objFSO
Set objFSO = _WScript.CreateObject("Scripting.FileSystemObject")
Set objFileHelper = New FileHelper
Set objFileHelper.FSO = objFSO
注意
The last line of code to FSO
use the Set statement when property assignment. This is necessary because the FileHelper
class is using Property Set
the process to the FSO
property assignment. If you do not begin with the last line of the Set statement, VBScript will error. If the class is based on the property of the object, it should generally Property Set
process. Most programmers are using class hope so.
1.3.5 Creating a read-only attribute
There are two ways to create a read-only property class:
- This property is only available for the
Property Get
process. - The
Property Get
process is declared as public, but theProperty Let
process is declared private.
1.3.6 create a write-only property
There are two ways to create a write-only property, these two methods and ways to create a read-only attribute exactly the opposite:
- Ignore Property Get procedure, provide only a Property Let procedure.
- Statement by Public Property Let declaration process, with Private Property Get procedure declaration statement
1.4 method definitions
Method (method) is actually another name for functions and procedures
in syntax with VBScript defined functions and procedures of the method is the same. The only additional consideration is to be declared as public or private
Class Greeting
Private mstrName
Public Property Let Name(strName)
mstrName = strName
End Property
Public Sub ShowGreeting(strType)
MsgBox MakeGreeting(strType) & mstrName & "."
End Sub
Private Function MakeGreeting(strType)
Select Case strType
Case "Formal"
MakeGreeting = "Greetings, "
Case "Informal"
MakeGreeting = "Hello there, "
Case "Casual"
MakeGreeting = "Hey, "
End Select
End Function
End Class
Outside the class code can call the ShowGreeting()
method, it is public; but you can not call the MakeGreeting()
method, because it is private, internal use only. Examples of the beginning of the script code using this class
Dim objGreet
Set objGreet = New Greeting
With objGreet
.Name = "Dan".ShowGreeting "Informal"
.ShowGreeting "Formal"
.ShowGreeting "Casual"
End With
Set objGreet = Nothing
1.5 class event
事件(event)
Is a special method will automatically be called. In any particular environment, the use of classes support one or several events. When a special method to support an event given environment, you can write an event handler (eventhandler), which in fact is called automatically when an event occurs- Any VBScript classes are automatically support two events:
Class_Initialize
andClass_Terminate
.
1.5.1 Class_Initialize event
When a code based on a class instance of the object class will trigger Class_Initialize
event. As long as it based on a class instance of an object, and this event will happen, but if it contains the code for this class responds depends entirely on himself. If you do not want to respond to this event, the event handler can ignore this event. The following example is a class containing Class_Initialize event processor.
Class FileHelper
' Private FileSystemObject object
Private mobjFSO
Private Sub Class_Initialize
Set mobjFSO = _
WScript.CreateObject("Scripting.FileSystemObject")
End Sub
'<<rest of the class goes here>>
End Class
- Technically, you can use the Public statement (rather than Private) declare the event handler, the event handler but are usually private. They can call if it is declared as public, outside the class code in the same way as others like to call it, which is usually not what we want.
- In a class you can have only one
Class_Initialize
event handler. If not, then you can not one, but certainly not more than one.
1.5.2 Class_Terminate event
Class_Terminate
Events and Class_Initialize
event contrary. When a class is triggered Class_Initialize event is instantiated as an object, and when the object of this class is based on the destruction of Class_Terminate event is triggered. There are two ways to destroy objects:
- The special value
Nothing
is assigned to the last reference to the object variable object - Beyond the last reference to that object object variable scope
When either of these conditions occurs, Class_Terminate event occurs before the object is actually destroyed.
Here is an example before seen FileHelper
class, now add an Class_Terminate
event handler.
Class FileHelper
' Private FileSystemObject object
Private mobjFSO
Private Sub Class_Initialize
Set mobjFSO = _
WScript.CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub Class_Terminate
Set mobjFSO = Nothing
End Sub
'<rest of the class goes here>
End Class
In this example, the event handler with Class_Terminate destroyed in the Class_Initialize
event instantiated objects. This is actually not necessary, because when FileHelper object is destroyed, private mobjFSO variable will be beyond its scope, the script engine will automatically be destroyed. However, some programmers like to explicitly destroy all instantiated object, which for example is still very useful.
You can also use the event Class_Terminate close the database connection, save and close the file, or some kind of information to a database or file. Syntax restrictions on Class_Initialize event handlers also apply to the event handler Class_Terminate
1.6 Construction and use of classes VBScript
CreateObject()
Function is only for 实例化非原生
the VBScript
object (such as Dictionary
and FileSystemObject
), but must New
be the same instance of a custom script VBScript 类
. The reason behind this is complex, so only need to remember this principle:
If an instance of an object, using a custom VBScript class New
; otherwise, useCreateObject
but there are exceptions, the regular target RegExp
must be usednew