none
DictionaryEntry does not work with Dictionary? RRS feed

  • Question

  • Copied the code from MS docs. Changed HashTable to Dictionary and it will not compile. Should this have been named HashTableEntry and not DictionaryEntry since it doesn't seem to work with a Dictionary? I know I can use KeyValuePair. I just wanted to investigate a new discovery.

    Private Sub myhash()
            ' Dim openWith As New Hashtable()
            Dim openWith As New Dictionary(Of String, String)
            openWith.Add("txt", "notepad.exe")
            openWith.Add("bmp", "paint.exe")
            openWith.Add("dib", "paint.exe")
            openWith.Add("rtf", "wordpad.exe")
            For Each de As DictionaryEntry In openWith
                Debug.Print("Key = {0}, Value = {1}",
                    de.Key, de.Value)
            Next de
    End Sub




    Mary

    Sunday, March 4, 2018 2:26 AM

All replies

  • Hi Mary,

    The link at the bottom is a good explanation between the two ways. 

    Try this

    Private Sub myhash()
        ' Dim openWith As New Hashtable()
        Dim openWith As New Dictionary(Of String, String)
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        For Each de As KeyValuePair(Of String,string)  In openWith
            Debug.Print("Key = {0}, Value = {1}",
                        de.Key, de.Value)
        Next de
    End Sub
    See also https://stackoverflow.com/questions/905424/keyvaluepair-vs-dictionaryentry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, March 4, 2018 2:45 AM
    Moderator
  • Thanks Karen but, as I said in my question, I know about KeyValuePair and have been using it for a while. Good link to SO but I always check them first. I think the docs on DictionaryEntry should say, right at the top, that DictionaryEntry does not work with Dictionary. Seems like a real misnomer in the framework.

    Mary

    Monday, March 5, 2018 2:08 AM
  • Thanks Karen but, as I said in my question, I know about KeyValuePair and have been using it for a while. Good link to SO but I always check them first. I think the docs on DictionaryEntry should say, right at the top, that DictionaryEntry does not work with Dictionary. Seems like a real misnomer in the framework.

    Mary

    I posted then realized you had tried the KeyValuePair. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, March 5, 2018 2:23 AM
    Moderator