帮助继承问题

编程入门 行业动态 更新时间:2024-10-21 06:09:27
本文介绍了帮助继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的情况.我有一个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.

更多推荐

帮助继承问题

本文发布于:2023-11-17 02:19:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!