CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2000
    Posts
    200

    Duplicate key in Dictionary - Better explanation!

    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





  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Duplicate key in Dictionary - Better explanation!

    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
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Sep 2000
    Posts
    200

    Re: Duplicate key in Dictionary - Better explanation!

    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.


  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Duplicate key in Dictionary - Better explanation!

    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
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Sep 2000
    Posts
    200

    Re: Duplicate key in Dictionary - Better explanation!

    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.


  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Duplicate key in Dictionary - Better explanation!

    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
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured