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 / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

Better way to implement removing an item from a List

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BobRoyAce - 05 Mar 2008 01:03 GMT
I have a class which has a private variable as follows (where
EntityAttachedDocument is another class in the project):

Private _EntityAttachedDocuments As List(Of EntityAttachedDocument)

In addition, I have two methods as follows:

Public Sub RemoveDocumentsToBeRemoved(ByVal sAttachdDocumentsFolder As
String)
 For Each oDoc As EntityAttachedDocument In _EntityAttachedDocuments
   If (oDoc.IsToBeRemoved = True) Then
     RemoveEntityAttachedDocument(oDoc, sAttachdDocumentsFolder)
   End If
 Next
End Sub

Public Sub RemoveEntityAttachedDocument( _
ByVal oEntityAttachedDocumentToRemove As EntityAttachedDocument, _
ByVal sAttachedDocsFolder As String)
 Dim sFilenameToRemove As String

 For Each oEntityAttachedDocument As EntityAttachedDocument In
EntityAttachedDocuments
   If (oEntityAttachedDocument.UniqueID =
oEntityAttachedDocumentToRemove.UniqueID) Then
     sFilenameToRemove = oEntityAttachedDocument.DocumentFilename

     EntityAttachedDocuments.Remove(oEntityAttachedDocument)

     ' If we're not dealing with a NEW Attached Doc, then delete the
corresponding EntityAttachedDocument record
     If (oEntityAttachedDocument.IsNew = False) Then
       oEntityAttachedDocument.Delete()
     End If

     ' Only attempt to delete the document if 1) sAttachedDocsFolder
is not blank, 2) document actually exists,
     ' and 3) IsNew = False (since, if IsNew = True, document has not
been copied to the folder yet).
     If (sAttachedDocsFolder.Length > 0) AndAlso
(_RemovedDocumentsShouldBeDeleted = True) AndAlso
(oEntityAttachedDocument.IsNew = False) Then
       Try
         If (My.Computer.FileSystem.FileExists(sAttachedDocsFolder &
sFilenameToRemove)) Then
           My.Computer.FileSystem.DeleteFile(sAttachedDocsFolder &
sFilenameToRemove, FileIO.UIOption.OnlyErrorDialogs,
FileIO.RecycleOption.SendToRecycleBin)
         End If
       Catch ex As Exception
         '...
       End Try
     Else
       '...
     End If
   End If
 Next

 ' If we got here, then we never found the EntityAttachedDocument, so
couldn't remove...
End Sub

The first method is attempting to remove from the
EntityAttachedDocument list all EntityAttachedDocument items
whose IsToBeRemoved property is true. To actually perform the removal,
the RemoveDocumentsToBeRemoved method
calls the RemoveEntityAttachedDocument method.

Well, this results in a runtime error (related to "collection was
modified") since the RemoveEntityAttachedDocument
method ends up removing an item from the list that
RemoveDocumentsToBeRemoved was iterating through. Now, I'm sure
that one solution would be to copy the code from the inside of the
For...Next loop in RemoveEntityAttachedDocument
and reproduce it in the For...Next loop in RemoveDocumentsToBeRemoved.
However, I don't like that and I'm thinking that there has to be a
better way.

Any suggestions?
SurturZ - 05 Mar 2008 02:39 GMT
Use a positional index rather than For..Each and go backwards instead.

For i as Integer = (_EntityAttachedDocuments.Count  - 1) to 0 Step -1
 Dim oDoc As EntityAttachedDocument = _EntityAttachedDocuments(i)
 if oDoc..IsToBeRemoved = True) Then
  RemoveEntityAttachedDocument(oDoc, sAttachdDocumentsFolder)
 End If
Next i

You might need to rewrite RemoveEntityAttachedDocument to use .RemoveAt
rather than .Remove, not sure.

Signature

David Streeter
Synchrotech Software
Sydney Australia

Spam Catcher - 05 Mar 2008 02:48 GMT
BobRoyAce <broy@omegasoftwareinc.com> wrote in news:370063c2-4b28-4f6e-
a222-da1454bde0b8@u72g2000hsf.googlegroups.com:

> Well, this results in a runtime error (related to "collection was
> modified") since the RemoveEntityAttachedDocument
[quoted text clipped - 5 lines]
> However, I don't like that and I'm thinking that there has to be a
> better way.

Iterate backwards, and remove items by index. Don't use a For Each Loop but
rather a regular For loop.

Signature

spamhoneypot@rogers.com (Do not e-mail)

Cor Ligthert[MVP] - 05 Mar 2008 04:45 GMT
Rob,

> Any suggestions?

Yea, make yourself a simple sample to test what you are doing with simple
datanames.
Probably that shows you the problem in a quick way, if not then you have
something to show us youe problem for a newsgroup more suitable way.

Cor

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.