Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Maintaining collection
Message
De
24/05/2013 06:43:10
 
 
À
24/05/2013 03:41:10
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01574508
Message ID:
01574633
Vues:
28
>>>>>>>Using a Dictionary < Tkey, TValue > would be better
>>>>>>
>>>>>>Thanks, I remember having read some of that in previous threads. I might have to consider that.
>>>>>
>>>>>
>>>>>You can update the value of a dictionary
>>>>>
>>>>>Here, I use a Tuple of three integers - for simplicity the first one is the key
>>>>>
>>>>>
>>>>>
>>>>>		public static void Main()
>>>>>		{
>>>>>
>>>>>
>>>>>			Dictionary<int, Tuple<int, int, int>> dict = new Dictionary<int, Tuple<int, int, int>>();
>>>>>
>>>>>			Tuple<int, int, int> t;  // key is tuple.Item1
>>>>>
>>>>>			t = new Tuple<int, int, int>(1, 1, 1);
>>>>>			UpdateDictionary(dict, t);
>>>>>
>>>>>			t = new Tuple<int, int, int>(2, 1, 1);
>>>>>			UpdateDictionary(dict, t);
>>>>>
>>>>>			PrintDictionary("Before any update", dict);
>>>>>
>>>>>			t = new Tuple<int, int, int>(2, 2, 2);
>>>>>			UpdateDictionary(dict, t);
>>>>>
>>>>>			PrintDictionary("After update", dict);
>>>>>			Console.ReadLine();
>>>>>		}
>>>>>		static void UpdateDictionary(Dictionary<int, Tuple<int, int, int>> dict, Tuple<int, int, int> t)
>>>>>		{
>>>>>			if (dict.ContainsKey(t.Item1) )
>>>>>			{
>>>>>				dict[t.Item1] = t;
>>>>>			}
>>>>>			else
>>>>>				dict.Add(t.Item1, t);
>>>>>		}
>>>>>		static void PrintDictionary(string msg, Dictionary<int, Tuple<int, int, int>> dict)
>>>>>		{
>>>>>			Console.WriteLine(msg);
>>>>>			foreach (KeyValuePair<int, Tuple<int, int, int>> x in dict)
>>>>>			{
>>>>>				Console.WriteLine("{0} {1} {2}", x.Value.Item1, x.Value.Item2, x.Value.Item3);
>>>>>			}
>>>>>
>>>>>		}
>>>>>
Since the key is in the data I'd consider using KeyedCollection instead of Dictionary ?
>>>>UPDATE : Assuming the key is an int.....
>>>
>>>Well - I made the key part of the data for simplicity - it doesn't need to be
>>>
>>>If you go for a KeyedCollection, wouldn't you need to subclass KeyedCollection first (and implement GetKeyForItem() ? http://msdn.microsoft.com/en-us/library/ms132454.aspx)
>>
>>Yes. Maybe not worth the candle but would be simpler when adding items etc......
>
>
>Candle or no candle
>
>I had not heard about KeyedCollection before and I am glad you brought it up - may be useful with the link Rob posted

Yeah, see my reply to Rob.....

>Adding the key to the data is something I do in vfp (washing out my mouth with soap)

Careful :-}

> so that the key is available in the foreach loop
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform