VBScript in class

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 Subboundary setting process similar statement, Class and End Class statement also applies to the boundary of the set class. Plurality may be used in a script Class … End Classblock 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 LetProcess is a special process for assignment to the private property variables outside the class. Property LetVBScript 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 或 Functionstatements to define this process, but the use of Property Let. Property LetProcess must receive at least one parameter. Without this parameter can not achieve Property Letthe 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 strNamesaving the value of the mstrNameprivate 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 the Property Letpurpose 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 Propertythe statement ending (like Function 过程必须以 End Functionthe end of Sub 过程必须以 End Subthe end of the same). If you want to jump out of property procedures, you can use Exit Propertythe statement (with a Exit Functionjump Function, with Exit Subout of the same Sub).

1.3.3 Property Get

Property GetProcess with the Property Letprocess is reversed. Property LetPrivate property is used to process variables outside the class code assignment, and Property Getthe process is allowed to read the value of the code external to the private property variables. Property GetFunction 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 VBScriptthe Functionprocess is similar to Property Getthe 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 corresponding Property Letname of the procedure is the same. Property LetThe value stored in the process variable private attributes, and Property Getthe process then it is read out.
  • Property GetProcess 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 the Property Let/Setparameters of the process than the corresponding parameter must be a Property Get procedure more. A Property Let/Setprocess 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 a Property Getprocess does require a parameter, it is best to add an additional property to achieve this Property Getparameter function. If the Property Get procedure returns a pointer to the object variable reference, you may need to Setreturn 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 SetProcess and Property Letthe process is very similar, but Property Setthe 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 a Property Setprocess of replacing a Property Letprocess. 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 Letand Property Setfunctional 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 = Objectwritten 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 FSOuse the Set statement when property assignment. This is necessary because the FileHelperclass is using Property Setthe process to the FSOproperty 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 Setprocess. 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 Getprocess.
  • The Property Getprocess is declared as public, but the Property Letprocess 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_Initializeand Class_Terminate.

1.5.1 Class_Initialize event

When a code based on a class instance of the object class will trigger Class_Initializeevent. 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_Initializeevent handler. If not, then you can not one, but certainly not more than one.

1.5.2 Class_Terminate event

Class_TerminateEvents and Class_Initializeevent 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 Nothingis 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 FileHelperclass, now add an Class_Terminateevent 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_Initializeevent 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 VBScriptobject (such as Dictionaryand FileSystemObject), but must Newbe 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 RegExpmust be usednew

Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/103433880