Panayot Karabakalov
Panayot Karabakalov

Reputation: 3179

What`s wrong in Property Let with 2 arguments?

Example code for illustration:

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

How to fix that? (and as bonus - why it`s happen?) Thanks

Upvotes: 0

Views: 356

Answers (1)

Cheran Shunmugavel
Cheran Shunmugavel

Reputation: 8459

Reference the documentation for Property Let. A Property Let is not the same as a subroutine call. The proper syntax is

obj.EnvFlags(0) = True

Upvotes: 3

Related Questions