Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Object error when adding to list

Hi,

I have these classes:

Public Class Party
    Public Property partyIdentifier As String
    Public Property email As String
    Public Property telephone As String
    Public Property role As String
End Class

Public Class Car
    Public Property registrationNumber As String
    Public Property registrationNumberMissing As Boolean
    Public Property model As String
    Public Property used As Boolean
    Public Property year As Integer
    Public Property mileage As Integer
    Public Property objectGroupType As String
End Class

Public Class Calculation
    Public Property cashPrice As Integer
    Public Property downPayment As Integer
    Public Property residualDebt As Integer
    Public Property creditTime As Integer
    Public Property interest As Double
End Class

Public Class Insurance
    Public Property monthlyCost As Integer
End Class

Public Class Finance
    Public Property contractType As String
    Public Property customerType As String
    Public Property customerLicenceType As String
    Public Property Parties As List(Of Party)
    Public Property car As Car
    Public Property calculation As Calculation
    Public Property profileId As Integer
    Public Property campaignId As Integer
    Public Property plannedDeliveryDate As DateTime
    Public Property insurance As Insurance
End Class

Open in new window


I'm now trying to build this as an object that I can serialize into json like this:
 If Not Page.IsPostBack Then
            Dim f As New Finance

            ' Basic
            f.contractType = "Installment"
            f.customerType = "Private"
            f.customerLicenceType = "SwedishDriversLicense"

            ' Parties
            Dim p As New Party With {
                .partyIdentifier = "8811119307",
                .email = "nordberg.peter@gmail.com",
                .telephone = "0854545656",
                .role = "Buyer"
            }
            f.Parties.Add(p)

            Dim json As String = JsonConvert.SerializeObject(f)
            Label1.Text = json
        End If

Open in new window


The problem is that I get an error that an object doesn't exist when adding an party item to the lost of parties as can be seen in below image.
 User generated image
What am I doing wrong and how can I fix it?

Peter
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Parties is not being initialized. Who created the Finance class? Maybe initialized this collection in the class constructor
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Nordberg

ASKER

Thanks!

Peter