?
Solved

Search Key in Dictionary

Posted on 2014-02-04
8
Medium Priority
?
406 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 20

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

862 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