vb.net - 如何在 vb.net 中将一个类附加到另一个相同的类
问题描述
PropertyPolicy 是一个定义多个字段/实体的集合的类。有时需要两个单独的函数来构建集合。(LoadEstateAIN 和 LoadAIN)。我需要结合两个类的结果,但尝试过 concat 但得到一个强制转换异常。什么在这里工作?
Public Class PropertyPolicy
Private agentfield As Entity
Private aininsuredfield() As Entity
Private billinginfofield As BillingInfo
Private cancellationdatefield As Date
Private claimsfield() As Claims
Public Property Agent As Entity
Get
Return Me.agentfield
End Get
Set(ByVal value As Entity)
Me.agentfield = value
End Set
End Property
Public Property AINInsured() As Entity()
Get
Return Me.aininsuredfield
End Get
Set(ByVal value As Entity())
Me.aininsuredfield = value
End Set
End Property
Public Property BillingInfo As BillingInfo
Get
Return Me.billinginfofield
End Get
Set(ByVal value As BillingInfo)
Me.billinginfofield = value
End Set
End Property
Public Property CancellationDate As Date
Get
Return Me.cancellationdatefield
End Get
Set(ByVal value As Date)
Me.cancellationdatefield = value
End Set
End Property
Public Property Claims() As Claims()
Get
Return Me.claimsfield
End Get
Set(ByVal value As Claims())
Me.claimsfield = value
End Set
End Property
End Class
Dim propTemp1 As New PropertyPolicy
Dim propTemp2 As New PropertyPolicy
Dim propTempComb As New PropertyPolicy
propTemp1.AINInsured = LoadEstateAIN(policyid, asofDate, lob, NINclientid, estatecompany)
propTemp2.AINInsured = LoadAIN(policyid, asofDate, lob, NINclientid, estatecompany)
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured)
解决方案
The result of Concat is not an array; it is an IEnumerable(Of T)
. In your case it is an IEnumerable(Of Entity)
. You just need to add ToArray()
to the end of the Concat if you want to assign it back to an array.
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured).ToArray()
Breaking down this line of code:
[instance3].[property] = [instance1].[property].Concat([instance2].[property])
Assigns the result of the Concat to the property, but the property is an array so you need to change the result of Concat which is an IEnumerable(Of Entity) into an array which is trivial with ToArray.
I could go further and recommend that you don't use arrays as public members, rather IEnumerable. Also, auto-properties would be a better choice for some of these Public/Public properties.
Public Class PropertyPolicy
Private aininsuredfield As Entity()
Private claimsfield As Claims()
Public Property Agent As Entity
Public Property BillingInfo As BillingInfo
Public Property CancellationDate As Date
Public Property AINInsured() As IEnumerable(Of Entity)
Get
Return aininsuredfield
End Get
Set(value As IEnumerable(Of Entity))
aininsuredfield = value.ToArray()
End Set
End Property
Public Property Claims() As IEnumerable(Of Claims)
Get
Return claimsfield
End Get
Set(value As IEnumerable(Of Claims))
claimsfield = value.ToArray()
End Set
End Property
End Class
And btw, that would cause your original code to work without ToArray()
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured)
推荐阅读
- vue.js - 如何在 Vuex 中使用突变来被动地创建或删除对象属性?
- android - 安装失败,原因是:“关闭”错误 | 安卓工作室 4.0.2
- python-3.x - 无余除法
- java - Java 垃圾收集器跨虚拟机的不同行为
- sql - SQL 中的 Powershell 漏洞评估
- php - 在 PHP 中计算一年中的最后一周以跳到明年
- ios - 无法将“String”类型的值转换为预期的参数类型“AnyObject?”
- ansible - ansible从变量逗号分隔创建字符串
- c++ - 允许可变参数的函数作为模板函数参数
- mysql - Typeorm“ER_BAD_FIELD_ERROR:'where 子句'中的未知列'entity.entityId'”