2015-10-13 60 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。