CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: foreach loops

  1. #1
    Join Date
    Dec 2008
    Posts
    31

    foreach loops

    Hi,

    I have a Lists of certain objects, and am looping through all combos of them like

    foreach(object o1 in theobjects){
    foreach(object o2 in theobjects){
    foreach(object o3 in the objects){
    }
    }
    }

    However I dont want to double up on myself, e.g. o1[element 1]o2[element 1]o3[element 2], can be regarded as the same as o1[element 1]o2[element 2]o3[element 1], (i.e. switching the last two etc).

    I just want to consider o1[element 1]o2[element 2]o3[element 3],o1[element 2]o2[element 1]o3[element 3],o1[element 3]o2[element 1]o3[element 2]

    Hope this makes sense...
    Last edited by Bobby_1234; January 18th, 2009 at 07:33 PM.

  2. #2
    Join Date
    Mar 2007
    Posts
    274

    Re: foreach loops

    Not a good answer i know, but something like this:

    Code:
     
    For each object x in objectList
    {
          trycast the object to its correct type then:
    
          if (new object = type 1)
          {}
          if (new object = type 2)
          {}
          .......
    }
    why are you using object anyway, so you have a List<object>? What is the base class for the object? List<baseclass> would be better.
    Last edited by Traps; January 18th, 2009 at 09:16 PM.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: foreach loops

    foreach implies you want to do something for each object. You dont.

    Code:
    for (int i=0; i<count-2;++i)
      for (int j=i+1; j<count-1; ++j)
         for (k=j+1;k<count ++k)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

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