Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Delete elements in .NET collections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
headware - 09 Aug 2005 18:12 GMT
A common issue that I see in the
microsoft.public.dotnet.framework.adonet group is that if you want to
delete a DataRow from a collection of DataRows (e.g. myTable.Rows), you
can't do this using a foreach loop like this:

foreach(DataRow row in myTbl.Rows)
  if(someCondition)
     row.Delete();

You must instead use a regular for loop and loop from back to front
like this:

for(int i = myTbl.Rows.Count - 1; i >= 0; i--)
  if(someCondition)
     myTbl.Rows[i].Delete();

I understand why you must do this. My question is whether this is a
standard situation in .NET collection classes.

I have an XML file that I'm processing using the DOM classes
(XmlDocument, XmlNode, etc.) and I need to loop through the children of
an XmlNode and call a function on each one that may or may not delete
that child. Of course, if I use a foreach loop or a for loop that loops
from front to back, the loop counter gets messed up when I delete a
node. It seems to work when I use the same solution as listed above
with the DataRows, but I wanted to confirm that it was a valid solution
in this case.

I can't find any Microsoft documentation regarding this so I was hoping
somebody would be able to point me to some or maybe there would be
somebody with some "inside" knowledge about this issue.

Thanks,
Dave
Lloyd Dupont - 10 Aug 2005 02:09 GMT
yep, it's a common situation
I find it annoying too, but makes sense....

>A common issue that I see in the
> microsoft.public.dotnet.framework.adonet group is that if you want to
[quoted text clipped - 30 lines]
> Thanks,
> Dave
headware - 12 Aug 2005 17:14 GMT
Of course, if Micorsoft changes the code so that when an item is
deleted from the list, it fills in the empty space from front to back
instead of back to front, that's going to break lots of code.
Peter Sestoft - 10 Aug 2005 07:35 GMT
> A common issue that I see in the
> microsoft.public.dotnet.framework.adonet group is that if you want to
[quoted text clipped - 11 lines]
>    if(someCondition)
>       myTbl.Rows[i].Delete();

This can be marvellously inefficient if the row set is large and more
than a few rows are to be deleted: the runtime is most likely
quadratic.

Peter

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.