23314

Dictionary makes value for all keys equal to most recent key/value addition

Question:

I'm trying to add some items to a dictionary object (database ID and value pair) to let the user confirm their selections before they commit. I have this following code section which add an item to the dictionary. After the add I loop through the Dictionary.Keys and print each key/value to a listbox for the user to see. My dictionary object is a public variable on my form and is set in the Form_Load event.

Dim PickListID As Integer If txtPercentOfStream <> "" Then PickListID = cboCoalTypes If Not CoalsInStreamDic.Exists(PickListID) Then CoalsInStreamDic.Add PickListID, txtPercentOfStream End If Else Exit Sub End If

The weird thing is that whenever I add a new key/value pair the value for the new key becomes the value for all existing keys in addition to the new key.

I run this little block before and after interacting with the dictionary

Dim key As Variant For Each key In CoalsInStreamDic.Keys Debug.Print key & "::" & CoalsInStreamDic.item(key) Next key Debug.Print

i.e. user clicks the button and the <em>first</em> think that happens is this above block to confirm the old keys have their original values, but they don't. They have already been replaced with the "new value". Running the block afterwards confirms this.

Why is this happening?

Answer1:

The problem is you're not adding the text of the TextBox, but rather the TextBox instead

If you change your code to the following it should work

If txtPercentOfStream <> "" Then Dim PickListID As Integer, PercentOfStream As Integer PercentOfStream = txtPercentOfStream PickListID = cboCoalTypes If Not CoalsInStreamDic.Exists(PickListID) Then CoalsInStreamDic.Add PickListID, PercentOfStream End If Else Exit Sub End If

Recommend

  • UAC Window On Top
  • Ms Access multi-select listbox mover
  • VB.NET default radio button selected inside a group box
  • tabcontrol selectedindex changed event not getting fired C#
  • Button inside Update Panel is not triggered, in asp.net
  • Passing Data between VB.NET forms
  • Pasting URLs That Have Been Scraped From a Webpage
  • Operation not supported on read-only collection
  • how to know the last row filled in vba (excel)? [duplicate]
  • Align Excel cell to center VB - xlCenter is not declared
  • How to create a data template dependent on an XML Attribute?
  • Bind collection of objects to ListBox
  • Double-click autofill - dynamic based on adjacent cell
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Creating a Multidimensional, Associative Array in VBScript
  • Conversion from string “a” to type 'Boolean' is not valid
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Why value captured by reference in lambda is broken? [duplicate]
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • jQuery tmpl and DataLink beta
  • How to format a variable of double type
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • Why joiner is not used after Sequence generator or Update statergy
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • Recursive/Hierarchical Query Using Postgres
  • reshape alternating columns in less time and using less memory
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • How can I use threading to 'tick' a timer to be accessed by other threads?