when using Action as a property (instead of a Field), calling the method causes error: Property access must assign to the property or use its value
when using Action as a property (instead of a Field), calling the method causes error: Property access must assign to the property or use its value
我将 StateProp() 定义为一个动作 属性 Public Property StateProp() As Action
.
调用 StateProp()
方法会导致错误:属性 access must assign to the 属性 or use its value.
当 Action 是 Field 而不是 属性 时执行相同的操作可以正常工作(请参阅示例代码的第一行)。 C# 中的相同代码可以正常工作,但在 VB...
中需要它
下面是问题的演示。在删除对 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
这是一个完整的答案,而不是我之前在评论部分给出的确实没有解决您的问题的简洁答案。
检索 属性 值在功能上等同于调用具有与 属性 相同 return 类型的函数(方法)。在这种情况下,它 return 是 System.Action
的一个实例,它是一个委托。委托具有您通常调用的 Invoke 方法。 VB.Net 允许您使用等同于 delegatename.Invoke()
的速记符号 delegateName()
。 VB.Net 还允许您在调用方法时省略空参数列表,我认为这导致了您对语法的一些混淆。
例如:
Sub DoSomething()
End Sub
..可以这样写来调用:
DoSomething
' or
DoSomething()
为什么这两种调用方法的方法之间的差异 DoSomething
相关?这是因为要让 VB.Net 理解您想在 return 从 属性-Get 函数编辑的 Action 实例上使用简写符号,您需要使用第二种形式post-pended '()' 后跟第二个 '()'。第一组结束方法调用,第二组告诉 VB 调用 Invoke 方法。请注意,如果该方法需要参数,则包含在“()”中的参数列表不能为空。
所以:
DelegateReturnedFromProperty()()
相当于:
DelegateReturnedFromProperty.Invoke
' or
DelegateReturnedFromProperty().Invoke()
这是一种风格上的偏好,但是 DelegateReturnedFromProperty()()
如果代码是由不完全精通 VB.Net 调用委托的语法糖的人审查的,这似乎只是在寻求混淆.
我将 StateProp() 定义为一个动作 属性 Public Property StateProp() As Action
.
调用 StateProp()
方法会导致错误:属性 access must assign to the 属性 or use its value.
当 Action 是 Field 而不是 属性 时执行相同的操作可以正常工作(请参阅示例代码的第一行)。 C# 中的相同代码可以正常工作,但在 VB...
下面是问题的演示。在删除对 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
这是一个完整的答案,而不是我之前在评论部分给出的确实没有解决您的问题的简洁答案。
检索 属性 值在功能上等同于调用具有与 属性 相同 return 类型的函数(方法)。在这种情况下,它 return 是 System.Action
的一个实例,它是一个委托。委托具有您通常调用的 Invoke 方法。 VB.Net 允许您使用等同于 delegatename.Invoke()
的速记符号 delegateName()
。 VB.Net 还允许您在调用方法时省略空参数列表,我认为这导致了您对语法的一些混淆。
例如:
Sub DoSomething()
End Sub
..可以这样写来调用:
DoSomething
' or
DoSomething()
为什么这两种调用方法的方法之间的差异 DoSomething
相关?这是因为要让 VB.Net 理解您想在 return 从 属性-Get 函数编辑的 Action 实例上使用简写符号,您需要使用第二种形式post-pended '()' 后跟第二个 '()'。第一组结束方法调用,第二组告诉 VB 调用 Invoke 方法。请注意,如果该方法需要参数,则包含在“()”中的参数列表不能为空。
所以:
DelegateReturnedFromProperty()()
相当于:
DelegateReturnedFromProperty.Invoke
' or
DelegateReturnedFromProperty().Invoke()
这是一种风格上的偏好,但是 DelegateReturnedFromProperty()()
如果代码是由不完全精通 VB.Net 调用委托的语法糖的人审查的,这似乎只是在寻求混淆.