C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
collection class problem,emergency help please Posted by AsAdi on 24 Jan 2006 at 1:19 PM
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
Report
Re: collection class problem,emergency help please Posted by weicco on 30 Jan 2006 at 1:08 AM
: "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:

foreach (MyType foo in Bar)
{
    Bar.Remove(foo);
}


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

// .NET 2.0
List<MyType> remove = new List<MyType>();
foreach (MyType foo in Bar)
{
    remove.Add(foo);
}
foreach (MyType foo in remove)
{
    Bar.Remove(foo);
}

Report
Re: collection class problem,emergency help please Posted by iwilld0it on 30 Jan 2006 at 7:11 AM
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#:

for(int i; i < Bar.length; ++i)
{
     Bar.Remove(i);
}


: : "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:
:
:
: foreach (MyType foo in Bar)
: {
:     Bar.Remove(foo);
: }
: 

:
: I've solved the problem like this but I don't know if this is the correct way though:
:
:
: // .NET 2.0
: List<MyType> remove = new List<MyType>();
: foreach (MyType foo in Bar)
: {
:     remove.Add(foo);
: }
: foreach (MyType foo in remove)
: {
:     Bar.Remove(foo);
: }
: 

:

Report
Re: collection class problem,emergency help please Posted by tsagld on 13 Feb 2006 at 5:27 AM
Small correction to your code, iwilld0it:
for(int i; i < Bar.length; ++i)
{
     Bar.Remove(i);
     i--;
}



: 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#:
:
:
: for(int i; i < Bar.length; ++i)
: {
:      Bar.Remove(i);
: }
: 

:
: : : "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:
: :
: :
: : foreach (MyType foo in Bar)
: : {
: :     Bar.Remove(foo);
: : }
: : 

: :
: : I've solved the problem like this but I don't know if this is the correct way though:
: :
: :
: : // .NET 2.0
: : List<MyType> remove = new List<MyType>();
: : foreach (MyType foo in Bar)
: : {
: :     remove.Add(foo);
: : }
: : foreach (MyType foo in remove)
: : {
: :     Bar.Remove(foo);
: : }
: : 

: :
:
:


Greets,
Eric Goldstein
www.gvh-maatwerk.nl




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.