Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Dictionary
Hi all,
can any one give me a sample how to update a value in dictionary based upon the key.
 
I have a collection of frames in a Dictionary
public Dictionary<int,frames> FrameID=new Dictionary<int,> ();
System .IO .DirectoryInfo myimagesdir=new System .IO.DirectoryInfo(@".\\tmp");
			
foreach(System .IO.FileInfo myimagesfile in myimagesdir.GetFiles("*.jpg"))
{
frameno=frameno+1;
FrameID.Add(frameno,new Frames());
}
 
Now i want to update frames based on the key. like for key=1 i want to add different frame(which is edited one).
how can i update old frame with new edited frame .

Thanks,
Posted 21-Oct-11 2:18am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Assumption: i is the key and, newFrame is the new value.

You can do it like the follwing:

if (FrameID.ContainsKey(i))
{
    FrameID.Remove(i);
}
 
FrameID.Add(i, newFrame);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

frames f = null;
if(FrameID.TryGetValue(frameno, out f))
{
   // frameno is in dictionary
   FrameID[frameno] = val;
}
else 
   FrameID.Add(frameno, val);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just treat it as an array.
This uses strings, but that's just because it is easier to see what is going on.
 
            Dictionary<int, string> dict = new Dictionary<int, string>();
            dict.Add(0, "zero");
            dict.Add(1, "one");
            dict.Add(2, "two");
            foreach (KeyValuePair<int, string> kvp in dict)
                {
                Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value);
                }
            dict[1] = "une";
            foreach (KeyValuePair<int, string> kvp in dict)
                {
                Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value);
                }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Maciej Los 280
2 BillWoodruff 240
3 Afzaal Ahmad Zeeshan 239
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 21 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100