向嵌套字典添加值

时间:2019-07-12 13:51:08

标签: vb.net reporting-services

当前,我正在尝试使用VB将值添加到嵌套字典中。我可以将其用于平面词典,但无法完全理解嵌套它的语法。

到目前为止,我已经评论了我遇到的问题:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="One" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Two" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Three" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Four" />

    </LinearLayout>

</LinearLayout>

我想做的是拥有一个结构如下的字典:

Public Shared Dim dictionary AS New System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.Dictionary(Of String, Integer))

Function addValue(ByVal code AS String, ByVal cust AS String,ByVal value AS Integer)
    Dim innerDict AS New System.Collections.Generic.Dictionary(Of String, Integer)
    innerDict.Add(cust,value);
    IF dictionary.ContainsKey(code) Then
            IF dictionary.Item(code).ContainsKey(cust) Then 'Can I access the Customer key in this way? 
                    dictionary.Item(code).Item 'Here I need to update the value held by customer to the old value + new value. 
                Else    
                 dictionary(code).Add(cust,value)  'Is this syntax correct? 
            End If

        Else
            dictionary.Add(code,innerDict)
    End If
End Function

1 个答案:

答案 0 :(得分:1)

这是可以执行您想要的功能的

它要做的第一件事是检查codedictionary的条目是否存在。如果不存在,它将添加一个值为空字典的字典,该字典将接收cust-value对。

当前,该函数不返回任何值。如果不返回任何值,则应使用Sub

Function addValue(ByVal code As String, ByVal cust As String, ByVal value As Integer)

    ' If no entry for code, create one.
    If Not dictionary.ContainsKey(code) Then
        dictionary.Add(code, New System.Collections.Generic.Dictionary(Of String, Integer))
    End If
    ' Add cust, value to entry at code.
    dictionary(code).Add(cust, value)

End Function

' Returns sum the customer's values.
Function SumCustomerValues(customer As String) As Integer
    Dim sum As Integer = 0
    For Each d As KeyValuePair(Of String, System.Collections.Generic.Dictionary(Of String, Integer)) In dictionary
        If d.Value.ContainsKey(customer) Then
            sum += d.Value(customer)
        End If
    Next
    Return sum
End Function