Click here to Skip to main content
14,327,007 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a List<classa> , it contains n elements . I want to separate the lists into two diff lists with small changes to the Item.

EX: List<classa> obj=new List<classa>();
It has some elements

i want to move even index entry in obj to List<classa> objEven , at the same time i want to move entries whose index is divisible by 4 to List<classa> objDiv4. while moving Change one property value to EVEN and Div4.

class Program
   {
       static void Main(string[] args)
       {
           List<classa> obj = new List<classa>();
           List<classa> objEven = new List<classa>();
           List<classa> objDiv4 = new List<classa>();
           int iCounter = 1;
           for (int i = 1; i <= 100; i++)
               obj.Add(new classA { ID = i, Notes = "Notes " + i.ToString() });

           classA temp = new classA();
           foreach (classA lobj in obj)
           {
               temp = lobj;
               if (iCounter % 2 == 0)
               {
                   temp.Notes = "EVEN";
                   objEven.Add(temp);
               }
               temp = lobj;
               if (iCounter % 4 == 0)
               {
                   temp.Notes = "DIV4";
                   objDiv4.Add(temp);
               }
               iCounter++;
           }

           foreach(classA lobj in objEven)
           {
               Console.Write(lobj.Notes+"  ");
           }
           Console.Read();
       }
   }

   class classA
   {
       public int ID { get; set; }

       public string Notes { get; set; }
   }

problem here objEven is containing elements with notes as DIV4.
Posted
Updated 31-Jan-15 7:51am
v3
Comments
Ramza360 31-Jan-15 14:20pm
   
The reason for this is that your setting temp to the same object, and this is creating a reference in the objEven list and objDiv4 list.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Your problem is that items with IDs which are dividable by 4 gets checked twice:
once if their ID is even and once if their ID is a multiple of 4 because the 2 if statements are unrelated.

Since every ID which is dividable by 4 is also dividable by 2 I'd place second if statement inside the first one like so:


if (iCounter % 2 == 0)
{
  if (iCounter % 4 == 0) // multiples of 4 will enter this scope
  {
      temp.Notes = &quot;DIV4&quot;;
      objDiv4.Add(temp);
  }
  else // other even numbers will enter this one
  {
    temp.Notes = "EVEN";
    objEven.Add(temp);
  }
}


That way items with even ID's get into either objDiv4 or ovjEven and necessarily not into both.
   
v2
Comments
JagadishBB 31-Jan-15 14:16pm
   
Thq for your Reply ...
Items which are divisible by 4 are also even numbers so it should exist in both objDiv4 and objEven but notes as different.
Ramza360 31-Jan-15 14:21pm
   
Try Solution2 :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

First off since your using a list, its easier to use the for instead of foreach.
Also my solution is assuming your don't care if the ID is even or not, just the index.

If you want to do by index, substitute the i % 2 and i % 4 with obj[i].ID.

classA temp = null;
for (int i = 0; i < obj.Count - 1; i++)
{
    temp = new classA();
    if (i % 2 == 0)
    {
        temp.ID = obj[i].ID;
        temp.Notes = "EVEN";
        objEven.Add(temp);
    }

    temp = new classA();
    if (i % 4 == 0)
    {
        temp.ID = obj[i].ID;
        temp.Notes = "DIV4";
        objDiv4.Add(temp);
    }
}
   
v4
Comments
JagadishBB 31-Jan-15 14:34pm
   
Thank You So much ...It solved my Problem. But i didn't understood when an object is modified and moved to some list . After that if we change it y it is reflecting in prev moved object. Below one dont have any problem , but with objects why is different
JagadishBB 31-Jan-15 14:35pm
   
ArrayList obj = new ArrayList();
ArrayList objEven = new ArrayList();
ArrayList objDiv4 = new ArrayList();

for (int i = 1; i <= 100; i++)
obj.Add(i.ToString() + " ");




int iCounter = 1;
foreach (string lobj in obj)
{
string str = lobj;
if (iCounter % 2 == 0)
{
str = str + " EVEN";
objEven.Add(str);
}
if (iCounter % 4 == 0)
{
str = str + " DIV4";
objDiv4.Add(str);
}
iCounter++;
}

foreach (string lobj in objEven)
{
Console.Write(lobj + " ");
}
Console.Read();
Ramza360 31-Jan-15 14:41pm
   
It was basically creating a reference to the classA object rather than a copy, so changing one, changes the other.
JagadishBB 31-Jan-15 14:50pm
   
Thq for th answer .
Why it is not happenning the same with string object.Any memory allocation side difference is there.
Ramza360 31-Jan-15 15:08pm
   
That's a good question, not entirely sure how to answer it. Perhaps someone else can enlighten us both there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100