Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

collection class problem,emergency help please

AsAdiAsAdi Posts: 117Member
Dear Sirs

I have class ,say MyLines,derived from CollectionBase,and I have implemented some collection functions
like Add,Remove,Item and Find in this class.
This class is enumerable and I can use foreach statement like this
foreach(Myline l in myLines)
{
......
}

But unfortunately I foun that after removing some objects from this collection,(from its innerlist)
this class isn't enumerable anymore and I can't use foreach statement and I receive this message:

"Collection was modified;enumeration operation may not execute"

how can I fix this problem?what's wrong with this collection class?here is my code for this class:

public class Lines:System.Collections.CollectionBase
{
private DLine mDLine;
private bool Check;
public Guid[,] mLineBack;

public BLines(bool WithDataSet)
{
if (WithDataSet)
this.mDLine=new DLine();
}
public BLines(Guid KeyUser)
{
mDLine=new DLine(KeyUser);
foreach(DataRow Dr in mDLine.mDTLine.Rows)
{
this.InnerList.Add(new Line((Guid)Dr["LineID"]));
}
}
public Line Add(Line lLine)
{
if (mDLine!=null)
{
if (mDLine.Add(lLine)=="")
Check=true;
this.InnerList.Add(lLine);
return lLine;
}
return null;
}
public Line Item(int Index)
{
if(this.Count==0)
return null;

if (Index!=-1)
return (Line) this.InnerList[Index];
else
return null;
}
public Line Item(Line lLine)
{
if(this.Count==0)
return null;

int i=this.InnerList.IndexOf(lLine);
if (i!=-1)
return (Line) this.InnerList[i];
else
return null;
}
public void Remove(Line lLine)
{
if (mDLine!=null)
{
if (mDLine.Delete(lLine)=="")
this.InnerList.Remove(lLine);
}
else
this.InnerList.Remove(lLine);
}
public void Remove(int Index)
{
Line lLine=(Line)this.InnerList[Index];
if (lLine!=null)
this.Remove(lLine);
}
public int Find(Line lLine)
{
int i=this.InnerList.IndexOf(lLine);
return i;
}
}

Thanks in advance
Reza

Comments

  • weiccoweicco Posts: 950Member
    : "Collection was modified;enumeration operation may not execute"
    :
    : how can I fix this problem?what's wrong with this collection class?here is my code for this class:

    This error comes when you are inside foreach-loop and change the collection. Like this:

    [code]
    foreach (MyType foo in Bar)
    {
    Bar.Remove(foo);
    }
    [/code]

    I've solved the problem like this but I don't know if this is the correct way though:

    [code]
    // .NET 2.0
    List remove = new List();
    foreach (MyType foo in Bar)
    {
    remove.Add(foo);
    }
    foreach (MyType foo in remove)
    {
    Bar.Remove(foo);
    }
    [/code]
  • iwilld0itiwilld0it Posts: 1,134Member
    You can not remove items from a collection while you are enumerating the collection w/ a foreach. Your code is pretty much the ONLY way to do it. If you want to remove items and enumerate in the same step, then you can do it by looping the old way#:

    [code]
    for(int i; i < Bar.length; ++i)
    {
    Bar.Remove(i);
    }
    [/code]

    : : "Collection was modified;enumeration operation may not execute"
    : :
    : : how can I fix this problem?what's wrong with this collection class?here is my code for this class:
    :
    : This error comes when you are inside foreach-loop and change the collection. Like this:
    :
    : [code]
    : foreach (MyType foo in Bar)
    : {
    : Bar.Remove(foo);
    : }
    : [/code]
    :
    : I've solved the problem like this but I don't know if this is the correct way though:
    :
    : [code]
    : // .NET 2.0
    : List remove = new List();
    : foreach (MyType foo in Bar)
    : {
    : remove.Add(foo);
    : }
    : foreach (MyType foo in remove)
    : {
    : Bar.Remove(foo);
    : }
    : [/code]
    :

  • tsagldtsagld Posts: 621Member
    Small correction to your code, iwilld0it:
    [code]
    for(int i; i < Bar.length; ++i)
    {
    Bar.Remove(i);
    i--;
    }
    [/code]


    : You can not remove items from a collection while you are enumerating the collection w/ a foreach. Your code is pretty much the ONLY way to do it. If you want to remove items and enumerate in the same step, then you can do it by looping the old way#:
    :
    : [code]
    : for(int i; i < Bar.length; ++i)
    : {
    : Bar.Remove(i);
    : }
    : [/code]
    :
    : : : "Collection was modified;enumeration operation may not execute"
    : : :
    : : : how can I fix this problem?what's wrong with this collection class?here is my code for this class:
    : :
    : : This error comes when you are inside foreach-loop and change the collection. Like this:
    : :
    : : [code]
    : : foreach (MyType foo in Bar)
    : : {
    : : Bar.Remove(foo);
    : : }
    : : [/code]
    : :
    : : I've solved the problem like this but I don't know if this is the correct way though:
    : :
    : : [code]
    : : // .NET 2.0
    : : List remove = new List();
    : : foreach (MyType foo in Bar)
    : : {
    : : remove.Add(foo);
    : : }
    : : foreach (MyType foo in remove)
    : : {
    : : Bar.Remove(foo);
    : : }
    : : [/code]
    : :
    :
    :


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

Sign In or Register to comment.