Solved

Search Key in Dictionary

Posted on 2014-02-04
8
308 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
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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now