VBS中Property Set和Property Let的区别

标签: , , ,

说好不玩VBS来着,但是今天有人问我,简单的写一下吧。

对于这个问题,《VBScript Programmers Reference》第215页说的很清楚:

Functionally, Property Let and Property Set procedures do the same thing. However, the Property Set procedure has two differences:

  • It makes it clearer that the property is an object-based property (any technique that makes the intent of the code more explicit is preferable over any other equally correct technique).
  • Code outside of your class must use the Set Object.Property = Object syntax in order to write to the property (also a good thing, because this is the typical way of doing things).

从功能上说,这两者的作用是一样的。但是Property Set有两点不同:第一,它说明了这个属性是一个与对象有关的属性;第二,在类的外面给属性赋值的时候必须使用Set关键字。

文件说明太抽象了,举个例子:

'Property Set
Class File
    Private m_fso
    Public Property Set fso(para_fso)
        m_fso = pata_fso
    End Property
End Class

Dim fso
Set fso = CreateObject("scripting.filesystemobject")
Set objFile = New File
'必须加上Set,否则报错
Set objfile.fso = fso
'Property Let
Class File
    Private m_fso
    Public Property Let fso(para_fso)
        m_fso = pata_fso
    End Property
End Class

Dim fso
Set fso = CreateObject("scripting.filesystemobject")
Set objFile = New File
'不能加上Set,否则报错
objfile.fso = fso

应该说明白了吧,继续Pythoning。

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. PTMaster,新的PT流量作弊工具?
  2. PowerISO 5.7 注册码
  3. CRC批量校验工具——RapidCRC
  4. OpenWrt端口转发设置
  5. RasEnumConnections函数返回632错误

3 条评论 发表在“VBS中Property Set和Property Let的区别”上

  1. test说道:

    不错, 明白了,自己的问题也解决了,谢谢……

  2. 叶子说道:

    不好意思有点疑问:
    1.既然set“它说明了这个属性是一个与对象有关的属性”,为什么第二个例子还可以在类中储存object呢?
    2.既然“在类的外面给属性赋值的时候必须使用Set关键字”,那为什么第二个例子没用set呢?

    • 叶子说道:

      是不是说
      1.可以在类中使用property let的object
      2.只有property set才必须用set赋值

留下回复