VBS object-oriented programming and me key words

Time:2019-11-2

VBS is a very simple scripting language, because of its simplicity, it only supports a small part of the object-oriented features.

Other object-oriented programming languages usually use this or self keywords to access the current object, while VBS uses me keywords. The me keyword represents the current instance, or object, of the class at code run time.

An object is an instance of a class. The same class can instantiate many objects with the same properties and methods, but each object is unique, and its member variables are not the same. These member variables are accessed by object references. Outside the class, each object is identified by a variable that stores the object reference. Inside the class, this variable cannot be accessed, but VBScript internally tracks which object is executing the class’s properties or methods, and obtains this information through the me keyword.

A simple class:

Copy codeThe code is as follows:
Class myClass
Private i_count

Public Property Get count
count = i_count
End Property
Public Property Let count(c)
i_count = c

End Property
End Class

In this example, a private variable is declared, which can be referenced inside the class. But sometimes, when we access a variable, we want a piece of code to be executed.

Use me keyword:

Copy codeThe code is as follows:
Class myClass
Private i_count

Public Property Get count
i_count = i_count + 1
count = i_count
End Property
‘Demon note:

‘actually, it’s just for me
‘direct countwire = count + count is simpler
Public Property Get countTwice
countTwice = Me.count + Me.count

End Property
Public Property Let count(c)
i_count = c
End Property
End Class

In this example, each time the count property is accessed, it is added with a 1.

Reference link:Self-referencing in VBScript

Reference link:VbScript Me Keyword

Original: http://demon.tw/programming/vbs-me-keyword.html

Recommended Today

Web API records the type of server address obtained by deploying IIS

There are many types of server address. The following records are available 1、HttpContext.Current.Server.MapPath(“~/File”) The returned value is D: \ 3project \ code \ mobileservice \ webapi \ file. Local service: this path is the root directory of the disk address where the project is located. Deployment server: the disk folder where the deployment file is […]