...

View Full Version : Accessing object inside object



davidd07
11-22-2011, 12:53 PM
Hi Guys,
I am new to .NET and finding it difficult to grasp at times. I have a list inside a dictionary however I am stuck at the point of getting the list values out of the container dictionary. Any help greatly apprecitated.


public Dictionary<int, object> Titles;

public Data()
{
Titles = new Dictionary<int, object>();
Titles.Add(1, new List<string>() { { "REGION" } });
Titles.Add(2, new List<string>() { { "REGION" }, { "STATE" } });

foreach (KeyValuePair<int, object> pair in Titles)
{
Debug.WriteLine("KEY: " + pair.Key + "VALUE :" + pair.Value + "\n");

// step further into list---------------?
.........ForEach(x => { Console.WriteLine(x); });
}

}

davidd07
11-23-2011, 01:48 AM
*anyone.
And I thought this question was realtively simple. :)

alykins
11-23-2011, 03:09 AM
there are not that many C# coders here (or at least I haven't seen many)... I haven't done something like this- sry :(

davidd07
11-23-2011, 05:12 AM
I think your right. I remmeber when I was learning PHP I used to come to this site, it helped me learn alot. We are talking about maybe 10years ago. :)

alykins
11-23-2011, 01:01 PM
don't get me wrong- I love it here and get a lot of help; but it seems to be predominately open source

Inigoesdr
11-23-2011, 07:30 PM
Here is how you would loop through it:

foreach (KeyValuePair<int, object> pair in Titles)
{
Console.WriteLine("KEY: " + pair.Key + "\n");

foreach (string str in pair.Value as System.Collections.IEnumerable)
{
Console.Write("VALUE: " + str + "\n");
}
}
This outputs:

KEY: 1

VALUE: REGION
KEY: 2

VALUE: REGION
VALUE: STATE

alykins
11-23-2011, 07:52 PM
it seems to me there is an issue with there being two values to a key... shouldn't the key have one value?

Inigoesdr
11-23-2011, 07:54 PM
it seems to me there is an issue with there being two values to a key... shouldn't the key have one value?

What? OP's code has two items in a List Collection for key 2.

alykins
11-23-2011, 08:14 PM
no i'm not arguing your code... was simply saying that isn't a key generally used with one value per key? i've never used Dictionary<int, object> (and maybe I am reading it wrong) but it seems to only take two arguments of key and value... how are two values getting thrown in there with no overload? (overload might be the wrong word)

Inigoesdr
11-23-2011, 08:19 PM
no i'm not arguing your code... was simply saying that isn't a key generally used with one value per key? i've never used Dictionary<int, object> (and maybe I am reading it wrong) but it seems to only take two arguments of key and value... how are two values getting thrown in there with no overload? (overload might be the wrong word)

Ah, I see what you are saying. Yes, the Dictionary here only takes one value per key, but the second value is an object, which OP is inserting as a List, which contains multiple elements itself.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum