我将StateProp()定义为动作属性Public Property StateProp() As Action
。
调用StateProp()
方法将导致错误:属性访问必须分配给属性或使用它的值。
当Action是一个字段而不是一个属性时,做同样的工作就可以正常工作(请参阅示例代码的第一行)。 在C#中的相同代码将工作得很好,但它需要它在VB中...当使用Action作为属性(而不是字段)时,调用该方法会导致错误:属性访问必须分配给属性或使用其值
下面是对问题的演示。它将不会编译,直到对StateProp()的调用被删除。 我该如何做这项工作?
Module Module1
Sub Main()
Dim State As Action
State = AddressOf DoSomething1
State()
State = AddressOf DoSomething2
State()
Console.ReadLine() 'all the above works fine.
StateProp = AddressOf DoSomething1
'StateProp() ' **************** uncommenting this call causes compilation error: Property access must assign To the Property Or use its value.
Console.ReadLine()
End Sub
'trying to use this bit fails.
Dim m_checkstate As Action
Public Property StateProp() As Action
Get
Return m_checkstate
End Get
Set
Console.WriteLine("Changing state. Do some work")
m_checkstate = Value
End Set
End Property
Sub DoSomething1()
Console.WriteLine("Something1")
End Sub
Sub DoSomething2()
Console.WriteLine("Something2")
End Sub
End Module
'StateProp.Invoke()' – TnTinMn
@TnTinMn感谢。请将它作为答案发布,以便我可以将其标记为已回答。 – Guy