?
Solved

Search Key in Dictionary

Posted on 2014-02-04
8
Medium Priority
?
400 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

770 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