CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    foreach loop parallel

    I need to loop two dictionary at the same time, like the for loop., how can i achieve this??

    Code:
    Dictionary<int,object> p_dictOld;
    Dictionary<int,object> p_dictNew;
    
    foreach (KeyValuePair<int, object> kvpOld in p_dictOld "&&" KeyValuePair<int, object> kvpONew in p_dictNew) 
    {
    kvpOld.key = ...
    kvpONew.key=...
    ........
    
    }

  2. #2
    Join Date
    Apr 2006
    Posts
    220

    Re: foreach loop parallel

    This can only be done in FOR loop and the lengths should be equal for both dictionaries.

    Correct me someone, if I am wrong.

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    Re: foreach loop parallel

    How can this be done in FOR loop, because the key value pair can be anything, and i want to retrieve all the key value pair in the dictionaries.

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: foreach loop parallel

    how big do you suspect these dictionaries to get? if it's fairly small(less than 100), then you can just loop through them individually. if they are going to be huge(10,000+), then you probably need to look into multi-threading.

  5. #5
    Join Date
    Jul 2002
    Posts
    788

    Re: foreach loop parallel

    COuld you show me how to use FOR loop to do this, since the index or key can be anything. Also, let's say my dictionary is

    Dicitonary<string, data>, how to use For loop to retrieve every pair of key, which is a string value and its corresponding data in the dictionary?

  6. #6
    Join Date
    Mar 2008
    Posts
    72

    Re: foreach loop parallel

    Code:
                Dictionary<string, int> dictionary1 = new Dictionary<string, int>();
                dictionary1.Add("1", 1);
                dictionary1.Add("2", 2);
                dictionary1.Add("3", 3);
                
                Dictionary<int, short> dictionary2 = new Dictionary<int, short>();
                dictionary2.Add(1, 1);
                dictionary2.Add(2, 2);
                dictionary2.Add(3, 3);
    
                Dictionary<string, int>.Enumerator enumerator1 = 
                    dictionary1.GetEnumerator();
                Dictionary<int, short>.Enumerator enumerator2 =
                    dictionary2.GetEnumerator();
    
                for (; ; )
                {
                    // LOGIC HERE
    
                    if (!enumerator1.MoveNext() || !enumerator2.MoveNext())
                    {
                        break;
                    }
                }
    You could change the logic to break out of the loop depending on whether the dictionaries have the same number of keys, or whether you want to go through every entry in both dictionaries, or just the number of entries in the smaller dictionary (as shown above). It's implementation dependent, but I'm sure you can figure it out.

    enumerator1.Current will give you the KeyValuePair for dictionary1.

  7. #7
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: foreach loop parallel

    you could also use a foreach loop.

    Code:
    Dictionary<string, string> dictionary1 = new Dictionary<string, string>();
    StringBuilder sb = new StringBuilder();
    
    foreach (KeyValuePair<string,string> pair in dictionary1)
    {
           sb.Append(string.Format("{0}:{1}", pair.Key, pair.Value);
           sb.Append(Environment.NewLine);
    }
    
    Console.WriteLine(sb.ToString()); // if you are doing a console app
    MessageBox.Show(sb.ToString()); // if you are doing a windows app

  8. #8
    Join Date
    Mar 2008
    Posts
    72

    Re: foreach loop parallel

    No, you can't use a foreach loop effectively for the problem the OP is trying to solve.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured