2013-03-12 33 views
0

用於說明實施例的代碼:有兩個參數讓Property有什麼問題?

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 

如何解決呢? (作爲獎金 - 爲什麼會發生?)謝謝

回答

3

參考文檔Property Let。 A Property Let與子程序調用不同。正確的語法是

obj.EnvFlags(0) = True 
+0

就是這樣,謝謝! – 2013-03-13 04:12:30