Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following code:

C#
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
Updated 1-Aug-12 11:33am
v2
Comments
Sergey Alexandrovich Kryukov 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 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 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.

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.
 
Share this answer
 
Comments
Member 8714829 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 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.
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:
C#
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!)
 
Share this answer
 
Comments
Christian Graus 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900