首页 > 解决方案 > 如何在 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)

标签: vb.netfunctionclassexceptionconcat

解决方案


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)

推荐阅读