New features in Vbsctipt 5.0

Source: Internet
Author: User
Tags eval expression new features regular expression
New features in Vbsctipt 5.0

Features that can be applied in ASP include those provided by the scripting engine, which means that VBScript improvements can also be applied in ASP. The improvements to VBScript are described below:

1, use the class in the script
The Complete VB Class (Class) model is implemented in VBScript, but the notable exception is the scripting events on the ASP server side. You can create classes in your scripts so that their properties and methods can be used with the rest of the code for the page, for example:
Class MyClass

Private m_halfvalue ' local variable to hold value of Halfvalue

Public Property Let Halfvalue (Vdata) "executed to set" Halfvalue property
If vdata > 0 Then m_halfvalue = vdata
End Property

Public Property Get Halfvalue () "executed to return" Halfvalue property
Halfvalue = M_halfvalue
End Property

Public Function GetResult () ' Implements the GetResult method
GetResult = M_halfvaue * 2
End Function
End Class

Set objthis = New MyClass

Objthis.halfvalue = 21

Response.Write "Value of Halfvalue property is" & Objthis.halfvalue & "<BR>"
Response.Write "Result of GetResult" to "& Objthis.getresult &" <BR> "
...
This code produces the following results:
Value of Halfvalue property is 21
Result of GetResult method is 42

2, with the structure
VBScript 5.0 supports the with structure, making code that accesses several properties or methods of an object more compact:
...
Set objthis = Server.CreateObject ("This.object")

With Objthis
. Property1 = "This value"
. Property2 = "Another value"
Theresult =. SomeMethod
End With
...

3. String evaluation
The Eval function (which used to be available only in JavaScript and JScript) is now supported in VBScript 5.0. Allows you to create a string that contains script code with a value of true or FALSE, and you can get a result after execution:
...
Datyourbirthday = Request.Form ("Birthday")
Strscript = "Datyourbirthday = Date ()"

If Eval (strscript) Then
Response.Write "Happy brithday!"
Else
Response.Write "Have a nice day!"
End If
...

4. Statement execution
The new Execute function allows script code in a string to be executed in the same way as the Eval function, but does not return a result. It can be used to dynamically create procedures that are executed later in code, such as:
...
Strcheckbirthday = "Sub checkbirthday (Datyourbirthday)" & Vbcrlf_
& "If Eval (Datyourbirthday = Date ()) Then" & Vbcrlf_
& "Response.Write" "Happy birthday!" "& Vbcrlf_
& "Else" & Vbcrlf_
& "Response.Write" "Have a nice day!" "& Vbcrlf_
& "End If" & Vbcrlf_
& "End Sub" & VbCrlf
Execute Strcheckbirthday
Checkbirthday (Date ())
...
A carriage return (as shown in the program) or a colon character ":" can be used to separate the statements in a string.

5. Set up area
The new setlocale method can be used to change the current region of the scripting engine, displaying special locale-specific characters, such as accent-accented characters or characters from different character sets.
Strcurrentlocale = GetLocale
SetLocale ("EN-GB")

6, Regular expression
VBScript 5.0 now supports regular expressions (which used to be available only in JavaScript, JScript, and other languages). RegExp objects are commonly used to create and execute regular expressions, such as:
Strtarget = "Test testing tested attest late Start"
Set objregexp = New RegExp ' Create a regular expression

Objregexp.pattern = "test*" ' Set the search pattern
Objregexp.ignorecase = False ' Set the case sensitivity
Objregexp.global = True ' Set the scope

Set colmatches = Objregexp.execute (strtarget) ' Execute ' Search

For each Match in Colmatches ' Iterate the Colmatches collection
Response.Write "Match found at position" & Match.firstindex & "."
Resposne.write "Matched value is '" & Match.value & "' .<br>"
Next
The results of the implementation are as follows:
Match found at position 0. Matched value is ' test '.
Match found at position 5. Matched value is ' test '.
Match found at position 13. Matched value is ' test ';
Match found at position 22. Matched value is ' test '.

7. Set up event handlers in client VBScript
This is not a scripting technique that is directly applied to ASP, and this new feature is useful when writing client-side VBScript. You can now dynamically specify a function or subroutine to associate with an event. For example, suppose the name of a function is MyFunction (), which can be assigned to the button's onclick event:
Function MyFunction ()
...
Function Implementation code here
...
End Function
...
Set Objcimbutton = document.all ("Cmdbutton")
Set Objcmdbutton.onclick = Get

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.