帮助继承问题 [英] Help with inheritance issues
问题描述
这是我的情况.我有一个BusinessObjectBase类.我的业务对象定义为:
Here is my situation. I have a BusinessObjectBase class. I have my business objects defined as:
public class Appointment
inherits businessObjectBase
然后,我创建了一个定义为
的基本集合类
Then I made a base collection class defined as
Public Class BusinessObjectCollectionBase(of T as {BusinessObjectBase})
inherits bindinglist(of T)
然后,将特定的集合类定义为
I then define my specific collection classes as
Public Class AppointmentCollection
inherits businessObjectCollectionBase(of Appointment)
为什么我不能执行以下操作:
why cant i do the following:
Dim Appcoll as AppointmentCollection
Dim Coll as businessObjectCollectionBase(of BusinessObjectBase) = Appcoll
我可以将对象定义为businessObjectBase并将其设置为约会对象,而不会出现任何问题,但是我认为对于集合而言,本质上是相同的事情是不允许的.我只是完全以错误的方式来解决这个问题,还是有办法解决这个问题?
i can define an object as a businessObjectBase and set it to an Appointment Object without any problems, but what i would think would be essentially the same thing for the collection is not allowed. Am i just totally going about this the wrong way, or is there a way to work around this?
推荐答案
是的,您不能这样做.我知道,很烂.
解决此问题的简单方法是将AddRange方法插入到BusinessObjectCollectionBase中:
Yeah, you can''t do that. I know, it sucks.
The easy way to work around is to throw in a AddRange method to your BusinessObjectCollectionBase:
Public Class BusinessObjectCollectionbase(Of T As BusinessObjectBase)
Inherits BindingList(Of T)
Public Sub AddRange(values As IEnumerable(Of T))
For Each Item As T In values
Me.Add(Item)
Next
End Sub
End Class
然后在您的代码中:
and then in your code:
Dim appointments As New AppointmentCollection
Dim businessBases As New BusinessObjectCollectionbase(Of BusinessObjectBase)
businessBases.AddRange(appointments)
中提琴!现在,两个集合都拥有对相同对象的引用,但它们具有不同的类型.
Viola! Both collections now hold references to the same objects but as differing types.
这篇关于帮助继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!