Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Dictionary
I have the following code:
 
Dictionary<string, object> dict= new Dictionary<string, object>();
 
someClass tmpClass = new someClass();
 
tmpClass.a = "blah";
tmpClass.b = "123";
tmpClass.c = "abc";
 
dict.Add("532Key", tmpClass);
 
// Now that the key and value (string and object) been assigned, how can I check if the ley is indeed "532Key" then get the values of tmpClass within the dictionary and process each element of the object assigning each to a class property (let call the new class newClass which have a,b, and c as properties). Thanks in advnce.
Posted 1-Aug-12 12:29pm
Edited 1-Aug-12 12:33pm
v2
Comments
Sergey Alexandrovich Kryukov at 1-Aug-12 17:35pm
   
Does not seem to make sense. Why checking the key? If it is "523Key", it will be it all the time. You are missing something.
What do you mean by "class property"? A static property? (as others are "instance properties")? "Process each element of the object assigning each to a class property" really sounds like "assign each element to itself".
--SA
Christian Graus at 1-Aug-12 17:45pm
   
He's clearly playing with a dictionary to understand the concept, not writing code he is going to use.
Member 8714829 at 1-Aug-12 18:35pm
   
hi Sergey, it will have other keys, I just used this key as an example. There is logic that will make use of the different keys in the dictionary that is why I have to do this sort of a check. the class I am using in my example have public strings/int that are using set and get.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

dict.ContainsKey lets you check if a key exists. You can iterate over dict.Keys and then look up each value as you go. But, you will struggle when you're using 'object', your best bet is to use the 'as' keyword to iterate over a list of possible types, and if you're doing that, you're better off strongly defining a class that contains those types. using string -> object will greatly complicate your code. string mapping to someClass would work way better.
  Permalink  
Comments
Member 8714829 at 1-Aug-12 17:41pm
   
Thank you for your feedback, I wonder if you have an example of how to use "as" in looping through the object to read and assign each element. I will place this "if" statment first:
if(dict.ContiansKey["532key"])
 
{
do the looping to read and assign the values in the "object" - but not sure how to do it
}
Christian Graus at 1-Aug-12 17:43pm
   
You would do something like
 
object o = dict["432key"];
 
someOtherObject soo = o as someOtherObject;
if (soo != null)
{
// It was of that type
}else
{
someObject so = o as someObject;
if (so != null)
{
// It was a someObject, so use it.
}
 
}
 
As you can see, this gets messy and ugly fast. Don't map to object if you can avoid it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First, if you know all values are going to be of type someClass, then change it from Dictionary<string, object> to Dictionary<string, someClass>. To access the value at "532Key", just use:
var value = dict["532Key"];
 
(Note: If you leave it as-is, you'll need to cast value to the proper type before you can access any of it's properties!)
  Permalink  
Comments
Christian Graus at 1-Aug-12 17:43pm
   
Of course, if he does not know for sure that this key exists, this will throw an exception if it does not.
lewax00 at 1-Aug-12 17:46pm
   
Of course. But that's true for any collection, you can't read an entry from an invalid index. I guess I assumed the asker would already know that much.
Christian Graus at 1-Aug-12 17:47pm
   
Yeah, but a dictionary is different, you know what your indices are in a list by getting the maximum. And he didn't seem to know about the Keys property.

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 1 Aug 2012
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