?
Solved

Search Key in Dictionary

Posted on 2014-02-04
8
Medium Priority
?
403 Views
Last Modified: 2014-02-05
Hi....

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      
endif
0
Comment
Question by:runnerjp2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 40
ID: 39833156
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
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 39833165
Hi,

dictionary("12345") = "something new"
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 39833270
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:runnerjp2005
ID: 39833751
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
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 39834068
runnerjp2005,

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
            MsgBox("BAAAD!")
        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.
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 39834072
Wait a second...Is what you really want to know whether a value (or a key) BEGINNING WITH "123" exists in the Dictionary?
0
 

Author Comment

by:runnerjp2005
ID: 39834096
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.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1500 total points
ID: 39835612
Use the Where extension method on the dictionary with BeginsWith string function

http://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question