• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Search Key in Dictionary


I would to check my Dictionary to see if the key contains a value so
so for example if i had a key in my dictionary that contains 12345 and i do this:

if Key contains "123" then

If Not dictionary.ContainsKey("12345") Then
           ' Update the key with new value
      end if      
1 Solution
Jacques Bourgeois (James Burger)PresidentCommented:
There is a contradiction in your question.

if i had a key in my dictionary that contains 12345
If Not dictionary.ContainsKey("12345") Then

If your dictionnary has a key that contains 12345 as in the first line, the code line would be useless because it would never trigger.


Try to explain your problem with other words and/or example
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:

dictionary("12345") = "something new"
Christopher KileCommented:
This code determines whether the key "12345" exists in the dictionary and, if not, gives it a value.
Dim dict as Dictionary(Of String, ValueType)
Dim newValue of New ValueType

If Not dict.ContainsKey("12345") then
    'first time key is given a value
    dict("12345") = newValue
End If

Open in new window

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

runnerjp2005Author Commented:
Sorry I need to  check if value contains it so value could be 123749958 bit if I'm searching for 123 it would bring up result as 123749958 contains 123
Christopher KileCommented:

That's impossible because neither ContainsKey() nor ContainsValue() works that way.

If what you said was true, the following code would produce one or two message boxes when executed - on my system, this code produces no message boxes:

Imports System.Collections.Generic

Module Module1

    Sub Main()
        Dim x As New Dictionary(Of String, String)

        x.Add("12345", "12345")
        If x.ContainsKey("123") Then
        End If
        If x.ContainsValue("123") Then
            MsgBox("VERY BAAAD!")
        End If
    End Sub

End Module

Open in new window

This code is also an exact demonstration about how to determine whether a particular key or particular value exist within a Dictionary.
Christopher KileCommented:
Wait a second...Is what you really want to know whether a value (or a key) BEGINNING WITH "123" exists in the Dictionary?
runnerjp2005Author Commented:
Yes that's correct ...I'm checking if it begins with something as I would like to change it... What I'm doingg is storing teams and score so hull 1 - 0 man u... If man u score I want to check the dictionary for any thing with hull in it... If it matches current score in this case no as the score is 1-1 update the key to reflect score...if score remains the same leave it.
Use the Where extension method on the dictionary with BeginsWith string function

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now