Vbscript Property let,Property Get,Property Set

摘要:
VBScript中的类属性用于为私有变量和数据验证过程分配值。PropertyLet:外部代码用于在私有属性变量中存储的值

Class properties in VBScript are used to assign values to private variable and handle the process of data validation.

Property Let: Which is used by the outside code to store a value in the private property variable. It is similar to a procedure in the sense that it does not return a value. A Property Let procedure must accept at least one argument. If the private variable you are using is an object then the process of assignment and data validation is handled by Property Set. Property Set: Similar to Property Let but used for object based properties. By default, the Property Set procedure is Public.

To retrieve the value of a private variable we will retrieve the value of a property. Property Get: This is used by code outside of your class to read the value of a private property variable. It is similar to a function in the sense that it returns a value to the calling code -- this value is the private variable value.

The Property Get procedure does not accept any arguments. You can add an argument to it, but then you have to add an additional argument to the property's corresponding Property Let or Property Set procedure, because Property Let/Set procedure must always have exactly one more argument than its corresponding Property Get procedure.

If the property get procedure returns an object then we can use the set statement (but it works well without set also) to return the value.

Class ABC
    'Private object
    Private var_obj

    Public Property Get username()
              Set username = var_obj       
    End Property
End Class

Read only Properties have only Property Get procedure

Write-only properties have only a Property Let or a Property Set procedure

Read-Write properties have a Property Get procedure and either a Property Let or a Property Set procedure

Example 1 of Property Let, Property Get, Property Set

Below Example, which shows a simple class that defines a private variable, m_var, and a two read-write properties, one_type and two_type, the latter of which is an object property.

Class Computer

   Private m_var
   Private o_var

   Public Property Let one_type(stringtype)
      m_var = stringtype
   End Property

   Public Property Get one_type(  )
      one_type = m_varv    End Property

   Public Property Set two_type(oObj)
      Set o_var = oObj
   End Property

   Public Property Get two_type(  )
      Set two_type = o_var
   End Property

End Class

Example 2 of Property Set

Here is the syntax for a Property Set procedure.

Class Main_class
    'Private FS_Object object
    Private var_Obj
    Public Property Set FSPro(objFSPro)
        Set var_Obj = objFSPro
    End Property
End Class

For example, here is what code that is using an object based on the above class might look like.

Dim objMain_class
Dim objFSPro
Set objFSPro = _WScript.CreateObject("Scripting.FS_Object")
Set objMain_class = New Main_class
Set objMain_class.FSPro = objFSPro

Last line uses the Set Statement when it writes to the FSPro property. This is required because the Main_class class used a Property Set procedure for the FSPro property. Without the Set statement at the beginning of the last line, VBScript would produce an error. When a property on a class is object based, it is typical to use a Property Set procedure. Most programmers using this class would expect this.

Example 3 of Property Set

For example imagine we had a class that contained a private property named ob_var_conn that was expected to be an ADO Connection object. This class definition, with the property Set and Property Get Statements might look like:

Class Connect_Class

'Create a private property to hold our Connection object
Private ob_var_conn
Public Property Get Connection()
Set Connection= ob_var_conn
End Property

Public Property Set Connection(ob_var_Connection)
'Assign the private property ob_var_conn to ob_var_Connection
Set ob_var_conn= ob_var_Connection
End Property
End Class

The end developer would use the Property Set statement in the following manner:

'Create an instance of Connect_Class
Dim ob_var_class, ob_var_record
Set ob_var_class= New Connect_Class
Set ob_var_Connection = Server.CreateObject('ADODB.Connection')
'Assign ob_var_Connection to the Connection property
Set ob_var_class.Connection = ob_var_Connection

As with the Property Let statement, the Property Set statement has an optional argument list. This argument list must be identical to the corresponding Property Get's argument list.

Example 4of Property Let, Property Get, Property Set

Class PencilClass

         Private recentPencil, recentColor

   Property Get Pencil()

            Set Pencil = recentPencil

   End Property

   Property Set Pencil(x)

            Set recentPencil = x

   End Property

   Property Get Pencilcolor()

            Select Case recentColor

                        Case 1: Pencilcolor = "Orange"

                        Case 2: Pencilcolor = "Green"

                        Case Else: Pencilcolor = "yellow"

            End Select

  End Property

   Property Let Pencilcolor(x)

            If x = "Orange" Then

               recentColor = 1

            Else

            If x = "Green" Then

               recentColor = 2

            Else

               recentColor = 0

            End If

            End If

  End Property

 End Class

 

 Set one_pencil = New PencilClass

 Set two_pencil= New PencilClass

 one_pencil.Pencilcolor = "Orange"

 wscript.echo one_pencil.Pencilcolor

 Set two_pencil.Pencil = one_pencil ' This invokes Property Set

 wscript.echo "1st time " & one_pencil.Pencilcolor

 two_pencil.Pencil.Pencilcolor = "Green"

 wscript.echo "2nd time " & one_pencil.Pencilcolor

轉自:http://www.editorial.co.in/software/vbscript_property_let_get_set.php

免责声明:文章转载自《Vbscript Property let,Property Get,Property Set》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇expect安装去测试asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

HTML5 Canvas 获取网页的像素值。

我之前在网上看过一个插件叫做出JScolor   颜色拾取器  说白了就是通过1*1PX的DOM设置颜色值通过JS来获取当前鼠标点击位置DOM的颜色值。 自从HTML5 画布出来之后。就有更好的方法来获取了,比方郭阿里巴巴ICON矢量库 用的SVG和渐变来进行绘制: 我昨天用Canvas来绘制了一下。由于Canvas有现成的方法getImageDat...

前端知识控制input按钮的显示与隐藏

1 if(fm.ReadFlag.value=="readonly"){ 2 var arr = document.getElementsByTagName("input") 3 for (var int =0; int < arr.length ; int ++){ 4 arr[int]....

执行异步任务,并记录时间

var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); try { DataTable dt = DbHelperSQL.Query("select CampaignCode from Test.dbo.[F2SMS04]...

[转]LINQ查询总结

-------适合自己的才是最好的!!! LINQ查询知识总结:案例分析 案例:汽车表car,系列表brand,厂商表productor private MyCarDataContext  _Context = new MyCarDataContext(); (1)查询全部汽车信息 var list = _Context.Car; LINQ语法:var...

NodeJS + Socket.IO 最终版

服务器端 //socket.io var app = require("express")(); var http = require("http").Server(app); var io = require("socket.io")(http); //couchbase var couchbase = require("couchbase"); va...

apache的安全增强配置(使用mod_chroot,mod_security)

 apache的安全增强配置(使用mod_chroot,mod_security)                                                                         作者:windydays      2010/8/17                                      ...