Click to See Complete Forum and Search --> : Duplicate key in Dictionary - Better explanation!
John Reynolds
July 4th, 2001, 09:35 PM
The FIRST key value is changing anytime tbLocNo changes. I don't understand why, but when the user enters a SECOND key value in the textbox, the FIRST key entry in the dict changes to that value. Thus, when the Add method is called, the key is already in the dictionary. (?)
Thanks for your help.
John R.
private Sub cmdOK_Click()
dictLocations.Add tbLocNo, tbLocDesc
If MsgBox("Add Another Location?", vbYesNo) = vbYes then
me.Hide
tbLocNo = ""
tbLocDesc = ""
me.Show
else
me.Hide
End If
End Sub
Clearcode
July 5th, 2001, 03:17 AM
A total guess - is tbLocNo declared as a Variant?
Try declaring it as a string instead.
(What may be happening is that tbLocNo is basically a pointer to string so what you are adding to the array - so when you then reset it to point at "" the reference already added to the dictionary changes )
HTH,
Duncan
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
John Reynolds
July 5th, 2001, 08:34 AM
Duncan, tbLocNo is the name of a text box on the form. Your explanation makes sense to me, but aren't all text boxes automatically a string?
The form is used only to input a location number (tbLocNo) and location description (tbLocNo). Then I am saving them to dictLocation (dictionary), so I can retrieve the description by referring to the key, which is the location number.
Appreciate any suggestions.
John R.
Clearcode
July 5th, 2001, 08:53 AM
A textbox is a class (of type textbox) whose default member is "Text As String".
However, if you add to the collection as per your original code you are adding a reference to the actual textboxes rather that their text members.
Change your code to:
private Sub cmdOK_Click()
dictLocations.Add tbLocNo.Text, tbLocDesc.Text
If MsgBox("Add Another Location?", vbYesNo) = vbYes then
me.Hide
tbLocNo.Text = ""
tbLocDesc.Text = ""
me.Show
else
me.Hide
End If
End Sub
Hope this helps,
Duncan
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
John Reynolds
July 5th, 2001, 03:56 PM
Duncan, EXCELLENT! You are 100% correct! Problem solved. I really don't quite understand why a reference to tbLocNo.Text isn't added to the dictionary . . . which would be a problem. I really thought that when a string was assigned to the tbLocNo it would go to the Text property. So, would you say when assigning the input from a textbox to anything (collection, dict, array, etc.) the reference should always be the text property of the textbox?
Thanks again for your help!
John R.
Clearcode
July 6th, 2001, 03:01 AM
The problem is with the default property (which for a text box is the Text property which is a string).
This confusing syntax will probably be phased out in VB.Net so it might be an idea to get into the habit of always fully qualifying the reference i.e. always assigning the string thus:
TextBox1.Text = ""
HTH,
Duncan
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.