Introduction to the use of the VBS class constructor and DEFAULT keyword _vbs

Source: Internet
Author: User
Tags constructor
In fact, the section on Function and SUB statements in the MSDN VBScript document mentions the Default keyword:
Copy Code code as follows:

Default
Used only and the public keyword in a Class blocks to indicate this Function procedure is the "default method" for the C Lass. An error occurs if more than one Default procedure be specified in a class.

Default can only be used with the Public keyword in a class statement block to indicate that a function procedure is the method of the class. If more than one procedure in a class is defined as Default, an error occurs.
A simple example:
Copy Code code as follows:

Class MyClass
Public Default Function SayHello (name)
SayHello = "Hello," & name
End Function
End Class
Set o = New MyClass
MsgBox O ("demon")

Many object-oriented languages can use constructors to initialize objects of a class, but the VBS does not have a constructor concept, but simply provides a class initialization event to initialize the object:
Copy Code code as follows:

Class TestClass
' Setup Initialize event.
Private Sub Class_Initialize
MsgBox ("TestClass started")
End Sub
' Setup Terminate event.
Private Sub Class_Terminate
MsgBox ("TestClass terminated")
End Sub
End Class
' Create an instance of TestClass.
Set X = New TestClass
' Destroy the instance.
Set X = Nothing

Although it looks like a constructor, it cannot take arguments, and there is no way to initialize an object with a specific parameter, as in other languages.
With the Default keyword, we can simulate the function of implementing a constructor:
Copy Code code as follows:

' Author:demon
' Date:2011/09/29
' website:http://demon.tw
Class Rectangle
Private Height, Width
Public Default Function Construtor (H, W)
Height = H:width = W
Set Construtor = Me
End Function
Public Property Get Area
Area = Height * Width
End Property
End Class
' Does it look like a constructor?
Set r = (New Rectangle) (6, 8)
MsgBox R.area

Reference Links: VBScript ' s default keyword
Original: http://demon.tw/programming/vbs-default-keyword.html

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.