Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing object inside object

    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.

    Code:
            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); }); 
                }
    
            }

  • #2
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *anyone.
    And I thought this question was realtively simple.

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    there are not that many C# coders here (or at least I haven't seen many)... I haven't done something like this- sry

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    don't get me wrong- I love it here and get a lot of help; but it seems to be predominately open source

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Here is how you would loop through it:
    Code:
                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:
    Code:
    KEY: 1
    
    VALUE: REGION
    KEY: 2
    
    VALUE: REGION
    VALUE: STATE

  • #7
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    it seems to me there is an issue with there being two values to a key... shouldn't the key have one value?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by alykins View Post
    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.

  • #9
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,760
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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)

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by alykins View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •