Note that there are some explanatory texts on larger screens.

plurals
  1. PODictionary with custom data-type values, unable to update the values
    primarykey
    data
    text
    <p>I am trying to iterate over a DataTable and separate out whether an opportunity resulted in a sale or not - but there are multiple avenues for the opportunity to happen, and I want to organise my output by user.</p> <p>I have a dictionary, the key being the user and the value field being a custom data structure. When adding keys/values initially things seem to behaving normally, but when updating values that already exist in the dictionary, the changes are not kept. My custom data type appears to be updating it's value internally, but when the function call exits, the value of the dictionary remains unchanged.</p> <p>Data structures being used are:</p> <pre><code>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 </code></pre> <p>The calling function, slightly simplified for clarity:</p> <pre><code> 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 </code></pre> <p><p><code>tmpDict(tmpName).Call.SalesLogged += 1</code> is a value and cannot be the target of an assignment - Is there a way to prevent the values of the dictionary from behaving like <code>ReadOnly</code> values?</p><p>Would the issue lie in the definition of my custom data types? Or should I be looking for a different approach to the problem altogether?</p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload