2015-10-13 21 views
0

我正在嘗試創建集合的副本,然後刪除原始集合。 我使用For Each循環遍歷原始集合,然後將對象添加到新集合中。 但我一旦從原始集合中刪除對象,我就無法使用新創建的集合。 伯爵在新創建的集合對象是正確的在每個循環中都可以從集合中獲取對象的副本

rMSPCalendar.Exceptions由MSProject每個日曆 提供此代碼後,如果疊代rMSPCalendarExceptions,我得到一個集合錯誤「對象需要」

Dim rMSPCalendarExceptions As New Collection 
Dim exception As Object 
rMSPCalendar.Exceptions(1). 
For Each exception In rMSPCalendar.Exceptions 
    rMSPCalendarExceptions.Add exception 
    exception.Delete 
Next exception 
+1

發佈您的編碼.... – Sathish

+0

當你說刪除,你的意思是你處置的對象?如果是這樣,你正在處理新集合中的引用指向的對象。記得VB使用引用。發佈您的代碼。 –

+0

它是爲MS項目,我添加了代碼 –

回答

1

相反比使用通用Collection對象,只與異常的名稱(例如,默認屬性)填充它,使用MSProject.Exception類型的列表:

Dim Exceptions As New List(Of MSProject.Exception) 
For Each Ex As MSProject.Exception In rMSPCalendar.Exceptions 
    Exceptions.Add(Ex) 
    Ex.Delete() 
Next 
For Each Ex In Exceptions 
    ' do something with the exception... 
    Console.WriteLine(Ex.Name & vbTab & Ex.Start & vbTab & Ex.Finish) 
Next 

BTW:由於Exception這個詞指的是一個本地的vb.net對象,我建議你將你的變量改爲更明確 - 也許是CalExceptions。