【问题标题】:how to append one class to another of same in vb.net如何在 vb.net 中将一个类附加到另一个相同的类
【发布时间】:2018-11-01 16:12:24
【问题描述】:

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)

【问题讨论】:

  • 您希望 Concat 做什么?你在看joining two arrays together吗?
  • 我希望它将两个类结果(来自两个单独的函数调用)结合在一起,形成一个新的单类结果。然后将结果序列化为 xml 并发送回另一个进程。我只是不确定使用什么来将同一类结构的两个实例基本合并为一个。也许这是不可能的,我只需要在处理第一个函数或其他东西的过程中调用第二个函数,但如果可能的话,我想加入结果。
  • “将两个班级的成绩结合在一起”是什么意思?例如,如果您在两个类中都有一个整数。比方说,3 和 5。结果应该是 8 吗? 35?别的东西?日期呢。
  • 在您的示例中,您尝试将属性连接在一起,它们是数组 - 而不是类实例本身。如果要加入实例,可以在类中编写一个方法,该方法采用类的实例并显式连接所有数组,并相应地处理非数组属性(例如,如何加入两个代理?)...如果这就是你想要做的......
  • 在这种情况下,一切都被定义为 PropertyPolicy 类结构。我正在调用两个单独的函数,它们返回此类的一个实例,并且只想组合结果,因此如果第一个函数返回一个由 2 个 aininsuredfield () 组成的数组,而第二个函数返回 1,则一个新的 propTempComb.AINInsured 组合实例( ) 将拥有所有 3。没有添加或任何东西,只有被保险实体的 3 次单独出现。我会尝试 .toarray()

标签: vb.net function class exception concat


【解决方案1】:

Concat 的结果不是数组;这是一个IEnumerable(Of T)。在您的情况下,它是IEnumerable(Of Entity)。如果要将其分配回数组,只需将 ToArray() 添加到 Concat 的末尾即可。

propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured).ToArray()

分解这行代码:

[instance3].[property] = [instance1].[property].Concat([instance2].[property])

将Concat的结果赋值给属性,但是属性是一个数组,所以你需要将一个IEnumerable(Of Entity)的Concat的结果改成一个ToArray微不足道的数组。

我可以更进一步,建议您不要将数组用作公共成员,而应使用 IEnumerable。此外,对于其中一些公共/公共属性,自动属性将是更好的选择。

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

顺便说一句,这将导致您的原始代码在没有 ToArray() 的情况下工作

propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured)

【讨论】:

  • 是的,添加 .toarray() 有效。在它从函数返回后,我不认为 aininsured() 将是一个数组,因此将其视为一个类实例,但在事后加入这些 .toarray() 就像一个魅力,xml 显示所有 3 个 aininsured () 非常感谢!!
  • @DanW aininsured() 几乎是一个数组的定义——当你的属性看起来像 Public Property AINInsured As Entity() / Public Property AINInsured() As Entity() 时,你的属性就是数组(注意属性名后面的第一个 () 是不必要的,只有第二个 () 使属性成为一个数组)。支持字段Private aininsuredfield() As Entity 是一个数组,但也可以写成Private aininsuredfield As Entity()not cool VB.Net, not cool...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2018-04-28
  • 2023-03-30
  • 2013-02-10
  • 2016-02-20
  • 1970-01-01
  • 2018-02-04
相关资源
最近更新 更多