Results 1 to 4 of 4

Thread: Searching value in Visual Basic dictionary object

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    4

    Question Searching value in Visual Basic dictionary object

    Dear all,

    I have a dictionary where I want to search for some text in the "Valuemember". I almost found how to do it but not completely what I want. If I specify the complete exact text, I get a match, but when I search for only a part of the value, I find nothing.

    Dim MyDictionary As New Dictionary(Of String, String)

    *** I fill my dictionary with following items: ***
    (Name1, 0001-1/2/3)
    (Name2, 0001-1/2/4)
    (Name3, 0001-1/2/5)

    If MyDictionary.ContainsValue("0001-1/2/3") then msgbox("Found it!")

    But I want to search for only a part of the valuemember, for example:
    If MyDictionary.ContainsValue("1/2/3") then msgbox("Found it!")

    Could anyone help me on this?

    Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Searching value in Visual Basic dictionary object

    Just to be clear, it's not a ValueMember. ValueMember is a property of a control like a ComboBox where, when you bind, the ValueMember property is set to the name of the column or property of the SelectedItem that should be exposed via the SelectedValue property. When talking about a Dictionary, there are keys and values. Literally, the Dictionary has a Keys property that is a collection of the keys and a Values property that is a collection of the values.

    The Dictionary has no inbuilt functionality to do what you want so you need to do it manually, basically as you would for any other collection, e.g.
    Code:
    If MyDictionary.Values.Any(Function(s) s.Contains("1/2/3")) Then
    That Any is a LINQ method that you could call on any IEnumerable(Of String).

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Searching value in Visual Basic dictionary object

    Here are some extension methods that you can use to search for partial matches in much the same as you do for full matches:
    Code:
    Imports System.Runtime.CompilerServices
    
    Public Module DictionaryExtensions
    
        <Extension>
        Public Function ContainsKey(Of TValue)(source As Dictionary(Of String, TValue), key As String, partialMatch As Boolean) As Boolean
            If partialMatch Then
                Return source.Keys.Any(Function(s) s.Contains(key))
            Else
                'Invoke instance method for full matches only.
                Return source.ContainsKey(key)
            End If
        End Function
    
        <Extension>
        Public Function ContainsValue(Of TKey)(source As Dictionary(Of TKey, String), value As String, partialMatch As Boolean) As Boolean
            If partialMatch Then
                Return source.Values.Any(Function(s) s.Contains(value))
            Else
                'Invoke instance method for full matches only.
                Return source.ContainsValue(value)
            End If
        End Function
    
    End Module
    Sample usage:
    Code:
    Module Module1
    
        Sub Main()
            Dim valuesByKey As New Dictionary(Of String, String) From {{"First", "One"},
                                                                       {"Second", "Two"},
                                                                       {"Third", "Three"}}
            'Instance method.
            Console.WriteLine(valuesByKey.ContainsKey("First"))
            Console.WriteLine(valuesByKey.ContainsKey("Three"))
            Console.WriteLine(valuesByKey.ContainsKey("irs"))
            Console.WriteLine(valuesByKey.ContainsKey("hre"))
            Console.WriteLine()
    
            'Extension method.
            Console.WriteLine(valuesByKey.ContainsKey("First", False))
            Console.WriteLine(valuesByKey.ContainsKey("Three", False))
            Console.WriteLine(valuesByKey.ContainsKey("irs", False))
            Console.WriteLine(valuesByKey.ContainsKey("hre", False))
            Console.WriteLine()
    
            'Extension method.
            Console.WriteLine(valuesByKey.ContainsKey("First", True))
            Console.WriteLine(valuesByKey.ContainsKey("Three", True))
            Console.WriteLine(valuesByKey.ContainsKey("irs", True))
            Console.WriteLine(valuesByKey.ContainsKey("hre", True))
            Console.WriteLine()
    
            'Instance method.
            Console.WriteLine(valuesByKey.ContainsValue("First"))
            Console.WriteLine(valuesByKey.ContainsValue("Three"))
            Console.WriteLine(valuesByKey.ContainsValue("irs"))
            Console.WriteLine(valuesByKey.ContainsValue("hre"))
            Console.WriteLine()
    
            'Extension method.
            Console.WriteLine(valuesByKey.ContainsValue("First", False))
            Console.WriteLine(valuesByKey.ContainsValue("Three", False))
            Console.WriteLine(valuesByKey.ContainsValue("irs", False))
            Console.WriteLine(valuesByKey.ContainsValue("hre", False))
            Console.WriteLine()
    
            'Extension method.
            Console.WriteLine(valuesByKey.ContainsValue("First", True))
            Console.WriteLine(valuesByKey.ContainsValue("Three", True))
            Console.WriteLine(valuesByKey.ContainsValue("irs", True))
            Console.WriteLine(valuesByKey.ContainsValue("hre", True))
    
            Console.ReadLine()
        End Sub
    
    End Module

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    4

    Re: Searching value in Visual Basic dictionary object

    jmcilhinney, thank you for your reply, it worked!

    Kind regards!

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