财产中有什么错误让2个参数?

时间:2013-03-12 23:30:34

标签: class properties vbscript syntax-error

示例代码示例:

Option Explicit
Dim obj: Set obj = New Foo
WScript.Echo "EnvFlags(0)=" & obj.EnvFlags(0) 'EnvFlags(0)=False
WScript.Echo Join(obj.EnvFlags(-1), ",")      'False,False,False

On Error Resume Next 'enabled just for facility's sake
obj.EnvFlags 0, True '<< Why this NOT work?...
If Err Then WScript.Echo Err.Number, Err.Description
'> 450 Wrong number of arguments or invalid property assignment
On Error Goto 0

Class Foo
    Private mEnvFlags

    Public Property Let EnvFlags(nIndex, bValue)
        If vbBoolean <> VarType(bValue) Then Exit Property
        If nIndex >= 0 And nIndex <= 2 Then
            mEnvFlags(nIndex) = bValue
        End If
    End Property

    Public Property Get EnvFlags(nIndex)
        If nIndex < 0 Or nIndex > 2 Then
            EnvFlags = mEnvFlags
        Else
            EnvFlags = mEnvFlags(nIndex)
        End If
    End Property

    Private Sub Class_Initialize
        mEnvFlags = Array(False, False, False)
    End Sub
End Class

如何解决这个问题? (作为奖励 - 为什么会发生?)谢谢

1 个答案:

答案 0 :(得分:3)

参考Property Let的文档。 Property Let与子程序调用不同。正确的语法是

obj.EnvFlags(0) = True