具有自定义数据类型值的字典,无法更新值

时间:2013-01-24 15:07:34

标签: vb.net data-structures dictionary

我正在尝试迭代DataTable并分离出机会是否导致销售 - 但是有机会发生多种途径,我想用户组织我的输出。

我有一个字典,关键是用户,值字段是自定义数据结构。当最初添加键/值时,事物似乎正常运行,但是当更新字典中已存在的值时,不会保留更改。我的自定义数据类型似乎是在内部更新它的值,但是当函数调用退出时,字典的值保持不变。

正在使用的数据结构是:

Public Structure ColleagueSlogs

  Public Call As LoggedItems
  Public Email As LoggedItems

End Structure

Public Structure LoggedItems

  Public SalesLogged As Integer

  Public Sub IncrementSales()
    Me.SalesLogged += 1
  End Sub

  Public NonSalesLogged As Integer

  Public Sub IncrementNonSales()
    Me.NonSalesLogged += 1
  End Sub

End Structure

为了清晰起见,稍微简化了调用函数:

  Protected Function SortData(ByVal data As DataTable) As Dictionary(Of String, ColleagueSlogs)

    Dim tmpDict As New Dictionary(Of String, ColleagueSlogs)

    For Each result As DataRow In data.Rows

      Dim tmpName As String = result.Item("UserID")

      If tmpDict.ContainsKey(tmpName) Then ''This block does not update correctly

        'this exists - increment the relevant variable
        Select Case result.Item("Origin")
          Case "Call"
            Select Case result.Item("Sale")
              Case "Yes"
                tmpDict(tmpName).Call.IncrementSales()
              Case "No"
                tmpDict(tmpName).Call.IncrementNonSales()
            End Select
          Case "Email"
            Select Case result.Item("Sale")
              Case "Yes"
                tmpDict(tmpName).Email.IncrementSales()
              Case "No"
                tmpDict(tmpName).Email.IncrementNonSales()
            End Select
        End Select

      Else ''This block works as expected

        'create data structure, increment the relevant var and add it to dict
        Dim tmpSlogs As New ColleagueSlogs
        Select Case result.Item("Origin")
          Case "Call"
            Select Case result.Item("Sale")
              Case "Yes"
                tmpSlogs.Call.IncrementSales()
              Case "No"
                tmpSlogs.Call.IncrementNonSales()
            End Select
          Case "Email"
            Select Case result.Item("Sale")
              Case "Yes"
                tmpSlogs.Email.IncrementSales()
              Case "No"
                tmpSlogs.Email.IncrementNonSales()
            End Select
        End Select

        tmpDict.Add(tmpName, tmpSlogs)

      End If

    Next

    Return tmpDict

  End Function

{p} tmpDict(tmpName).Call.SalesLogged += 1是一个值,不能作为赋值的目标 - 有没有办法防止字典值的行为类似于ReadOnly

问题在于我的自定义数据类型的定义吗?或者我应该完全寻找一种解决问题的方法吗?

0 个答案:

没有答案