Click to See Complete Forum and Search --> : foreach loops


Bobby_1234
January 18th, 2009, 06:23 PM
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...

Traps
January 18th, 2009, 07:35 PM
Not a good answer i know, but something like this:


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.

TheCPUWizard
January 18th, 2009, 08:33 PM
foreach implies you want to do something for each object. You dont.


for (int i=0; i<count-2;++i)
for (int j=i+1; j<count-1; ++j)
for (k=j+1;k<count ++k)