我尝试创建一个包含字典的属性。
Private _dic As Dictionary(Of String, Decimal)
Public Property DicProp(ByVal val1 As Decimal, ByVal val2 As Decimal,
ByVal val3 As Decimal) As Dictionary(Of String, Decimal)
Get
Return _dic
End Get
Set(value As Dictionary(Of String, Decimal))
value.Add("Value1", val1)
value.Add("Value2", val2)
value.Add("Value3", val3)
End Set
End Property
我试图用
填充属性 .DicProp(1,2,3)
但是我收到了消息"Property access must assign to the property or use its value"
。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
您希望函数(在本例中为VB)不是属性,因为属性只能获取或设置单个值。
Public Sub AddValues(ByVal val1 As Decimal, ByVal val2 As Decimal, ByVal val3 As Decimal)
_dic.Add("Value1", val1)
_dic.Add("Value2", val2)
_dic.Add("Value3", val3)
End Sub
然后:
AddValues(1,2,3)
答案 1 :(得分:1)
完全删除属性的Set
部分:
Private _dic As New Dictionary(Of String, Decimal)
Public Property DicProp As Dictionary(Of String, Decimal)
Get
Return _dic
End Get
End Property
即使没有setter,您仍然可以对此属性进行更改!
MyObject.DicProp.Add("Value1", 1.0D)
MyObject.DicProp.Add("Value2", 2.0D)
MyObject.DicProp.Add("Value3", 3.0D)
这很有效,因为它等同于这段代码:
Dim temp As Dictionary(Of String, Decimal)
temp = MyObject.DicProp ' Use the *Get* portion of the property in your object to retrieve the dictionary
temp.Add("Value1", 1.0D) ' Then use the Add (or Set properties) on the retrieved dictionary object