0

PropertyPolicy is a class that defines a collection of several fields/entities. Sometimes two separate functions are needed to build out the collection. (LoadEstateAIN and LoadAIN). I need to combine the results of both classes but have tried concat but get a cast exception. What would work here?

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)
Benno
  • 2,534
  • 1
  • 17
  • 27
DanW
  • 9
  • 1
  • What are you expecting Concat to do? Are you looking at [joining two arrays together](https://stackoverflow.com/questions/59217/merging-two-arrays-in-net)? – the_lotus Nov 01 '18 at 16:35
  • I was hoping it would join the two class results together (from two separate function calls) into a new single class result. The result is then serialized to xml and sent back to another process. I am just not sure what to use to join basically two instances of the same class structure into one. Maybe this is not possible and I just need to call the 2nd function during processing of the first function or something but I would like to join the results if possible. – DanW Nov 01 '18 at 16:45
  • What does "join the two class results together" mean ? For example, if you have an integer in both class. Lets say, 3 and 5. Should the result be 8? 35? something else? What about dates. – the_lotus Nov 01 '18 at 16:49
  • In your example you are trying to join the properties together, which are arrays - not the class instances themselves. If you want to join the instances you could write a method in the class which takes an instance of the class and concatenates all the arrays explicitly, and handles non array properties accordingly (how do you join two agents, for example?)... if that is what you are trying to do... – djv Nov 01 '18 at 16:51
  • in this case everything is defined as the PropertyPolicy class structure. I am calling two separate functions that return an instance of this class and just want to combine the results so if the 1st function returns an array of 2 aininsuredfield () and the 2nd function returns 1, then a new combined instance of propTempComb.AINInsured () would have all 3. There is no adding or anything, just 3 separate occurrances of the aininsured entity. I will try the .toarray() – DanW Nov 01 '18 at 16:56
  • @DanW The two functions you show don't return an instance of the class, rather an array of Entity. – djv Nov 01 '18 at 17:03
  • @DanW I understand now. The terms you use are a bit confusing. The ToArray() solution should do it. – the_lotus Nov 01 '18 at 17:06

1 Answers1

0

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)
djv
  • 15,168
  • 7
  • 48
  • 72
  • Yay adding .toarray() worked. I did not think after it returns from the function that aininsured() would be an array so was looking at it as a class instance, but joining these after the fact with .toarray() worked like a charm and the xml shows all 3 aininsured () Thanks so much!! – DanW Nov 01 '18 at 17:01
  • @DanW `aininsured()` is almost the definition of an array - your properties are arrays when they look like `Public Property AINInsured As Entity()` / `Public Property AINInsured() As Entity()` (note the first () after the property name is unnecessary, only the second () makes the property an array). And the backing field `Private aininsuredfield() As Entity` is an array, but it could also be written `Private aininsuredfield As Entity()`. [not cool VB.Net, not cool...](https://stackoverflow.com/a/28195263/832052) – djv Nov 01 '18 at 17:08