none
Query on Dictionary: Collections RRS feed

  • Question

  • Private Sub IterateThroughDictionary()  
        Dim elements As Dictionary(Of String, Element) = BuildDictionary()  
    
        For Each kvp As KeyValuePair(Of String, Element) In elements  
            Dim theElement As Element = kvp.Value  
    
            Console.WriteLine("key: " & kvp.Key)  
            With theElement  
                Console.WriteLine("values: " & .Symbol & " " &  
                    .Name & " " & .AtomicNumber)  
            End With  
        Next  
    End Sub  
    
    Private Function BuildDictionary() As Dictionary(Of String, Element)  
        Dim elements As New Dictionary(Of String, Element)  
    
        AddToDictionary(elements, "K", "Potassium", 19)  
        AddToDictionary(elements, "Ca", "Calcium", 20)  
        AddToDictionary(elements, "Sc", "Scandium", 21)  
        AddToDictionary(elements, "Ti", "Titanium", 22)  
    
        Return elements  
    End Function  
    
    Private Sub AddToDictionary(ByVal elements As Dictionary(Of String, Element),  
    ByVal symbol As String, ByVal name As String, ByVal atomicNumber As Integer)  
        Dim theElement As New Element  
    
        theElement.Symbol = symbol  
        theElement.Name = name  
        theElement.AtomicNumber = atomicNumber  
    
        elements.Add(Key:=theElement.Symbol, value:=theElement)  
    End Sub  
    
    Public Class Element  
        Public Property Symbol As String  
        Public Property Name As String  
        Public Property AtomicNumber As Integer  
    End Class

    I got the code from the following MSDN page:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections

    I am not able to understand the following points:

    a) In the AddtoDictionary subroutine, there is a line as follows:

    elements.Add(Key:=theElement.Symbol, value:=theElement)

    My question is: What is the role of the words "Key" and "Value"? IS it wrong if I omit them (for example can I write it as: elements.Add(theElement.Symbol, theElement)? Or does it have anything to do with the "KeyValuePair" keyword that has been used?

    b) In the sub "Iterate Through Dictionary", the following is present:

    For Each kvp As KeyValuePair(Of String, Element) In elements

    My Question: What is the role of "KeyValuePair"? Can someone give a detailed explanation...or better still if you can provide me with a link to some good literature on the same?


    Sougata Ghosh

    Thursday, July 12, 2018 6:25 PM

Answers

  • Hi

    There isn't much to be said. This example shows 2 ways to add the same items to a Dictionary(Of String, Integer).

    The keyValuePair refers to a Dictionart 'Item' which is composed of the Key and the Value. It can sometimes be useful to iterate through a Dictionary collection by using the KeyValuePair.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MyDict As New Dictionary(Of String, Integer)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' these two loops load the same
        ' items to MyDict.
        ' Both wouldn't be used, only
        ' one of them needed.
        For i As Integer = 1 To 10
          ' NOTE: the keys must be unique
          ' the Key being the first of
          ' the key/value pair.
          MyDict.Add(i.ToString, i)
        Next

        ' OR, alternatively
        'For i As Integer = 1 To 10
        '  ' NOTE: the keys must be unique
        '  ' the Key being the first of
        '  ' the key/value pair.
        '  MyDict.Add(key:=i.ToString, value:=i)
        'Next

        For Each kvp As KeyValuePair(Of String, Integer) In MyDict
          Dim key As String = kvp.Key
          Dim Value As Integer = kvp.Value
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 12, 2018 7:19 PM
    • Proposed as answer by KarenPayneOregonWork Thursday, July 12, 2018 7:33 PM
    • Marked as answer by sougata12 Sunday, July 15, 2018 8:30 AM
    Thursday, July 12, 2018 7:13 PM

  • a) In the AddtoDictionary subroutine, there is a line as follows:

    elements.Add(Key:=theElement.Symbol, value:=theElement)

    My question is: What is the role of the words "Key" and "Value"? IS it wrong if I omit them (for example can I write it as: elements.Add(theElement.Symbol, theElement)? Or does it have anything to do with the "KeyValuePair" keyword that has been used?

    b) In the sub "Iterate Through Dictionary", the following is present:

    For Each kvp As KeyValuePair(Of String, Element) In elements

    My Question: What is the role of "KeyValuePair"? Can someone give a detailed explanation...or better still if you can provide me with a link to some good literature on the same?


    Sougata Ghosh

    A)

    The := is a method of specifying the specific parameter name before the value.  This is typically only used with an method has optional parameters and you are skipping some of them; this allows the compiler to match the correct parameter value with the parameter name when the order of parameters does not match the method definition.

    The short answer is that they are not needed in this case.  You can simply use elements.Add(theElement.Symbol, theElement).  As long as your parameter values match the number and order of the parameters in the method signature, there is no need to include the parameter name and := operator.

    B)

    The Dictionary(Of TKey, TValue) contains elements of type KeyValuePair(TKey, TValue).  The purpose of the KeyValuePair is to provide the dictionary with a single storage object which contains both the key and value objects.

    You can always lookup any framework object on the docs browser site: https://docs.microsoft.com/en-us/dotnet/api/?view=netframework-4.7.1


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by sougata12 Sunday, July 15, 2018 8:30 AM
    Thursday, July 12, 2018 7:55 PM
    Moderator

All replies

  • Hi

    There isn't much to be said. This example shows 2 ways to add the same items to a Dictionary(Of String, Integer).

    The keyValuePair refers to a Dictionart 'Item' which is composed of the Key and the Value. It can sometimes be useful to iterate through a Dictionary collection by using the KeyValuePair.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim MyDict As New Dictionary(Of String, Integer)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' these two loops load the same
        ' items to MyDict.
        ' Both wouldn't be used, only
        ' one of them needed.
        For i As Integer = 1 To 10
          ' NOTE: the keys must be unique
          ' the Key being the first of
          ' the key/value pair.
          MyDict.Add(i.ToString, i)
        Next

        ' OR, alternatively
        'For i As Integer = 1 To 10
        '  ' NOTE: the keys must be unique
        '  ' the Key being the first of
        '  ' the key/value pair.
        '  MyDict.Add(key:=i.ToString, value:=i)
        'Next

        For Each kvp As KeyValuePair(Of String, Integer) In MyDict
          Dim key As String = kvp.Key
          Dim Value As Integer = kvp.Value
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 12, 2018 7:19 PM
    • Proposed as answer by KarenPayneOregonWork Thursday, July 12, 2018 7:33 PM
    • Marked as answer by sougata12 Sunday, July 15, 2018 8:30 AM
    Thursday, July 12, 2018 7:13 PM

  • a) In the AddtoDictionary subroutine, there is a line as follows:

    elements.Add(Key:=theElement.Symbol, value:=theElement)

    My question is: What is the role of the words "Key" and "Value"? IS it wrong if I omit them (for example can I write it as: elements.Add(theElement.Symbol, theElement)? Or does it have anything to do with the "KeyValuePair" keyword that has been used?

    b) In the sub "Iterate Through Dictionary", the following is present:

    For Each kvp As KeyValuePair(Of String, Element) In elements

    My Question: What is the role of "KeyValuePair"? Can someone give a detailed explanation...or better still if you can provide me with a link to some good literature on the same?


    Sougata Ghosh

    A)

    The := is a method of specifying the specific parameter name before the value.  This is typically only used with an method has optional parameters and you are skipping some of them; this allows the compiler to match the correct parameter value with the parameter name when the order of parameters does not match the method definition.

    The short answer is that they are not needed in this case.  You can simply use elements.Add(theElement.Symbol, theElement).  As long as your parameter values match the number and order of the parameters in the method signature, there is no need to include the parameter name and := operator.

    B)

    The Dictionary(Of TKey, TValue) contains elements of type KeyValuePair(TKey, TValue).  The purpose of the KeyValuePair is to provide the dictionary with a single storage object which contains both the key and value objects.

    You can always lookup any framework object on the docs browser site: https://docs.microsoft.com/en-us/dotnet/api/?view=netframework-4.7.1


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by sougata12 Sunday, July 15, 2018 8:30 AM
    Thursday, July 12, 2018 7:55 PM
    Moderator