Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm having a problem to add an array of contacts to a json object.

My class is:
Public Class HistoryClass

    Public Class Contact
        Public Property id As String
        Public Property displayName As String
    End Class

    Public Class History
        Public Property regarding As String
        Public Property startTime As DateTime
        Public Property endTime As DateTime
        Public Property contacts As Contact()
    End Class

End Class

I can successfully POST this json:
Dim h As New HistoryClass.History With {.regarding = "test", .startTime = Now, .endTime = Now}


But how do I add contacts to this?

What I have tried:

h.contacts = New List(Of Contact) With {.id = ContactID}

error: id is not a member of list(of HistoryClass.Contact

h.contacts = New Contact With {.id = ContactID}

error: Value of type HistoryClass.Contact cannot be converted to HistoryClass.Contact()


Dim contactColl As Collection = New Collection
       Dim ContactIDlijst As Dictionary(Of String, Contact) = New Dictionary(Of String, Contact)

       ContactIDlijst.Add("id", st)
       contactColl.Add(ContactIDlijst)
       h.contacts = contactVerzameling

error: Value of Collection cannot be converted to HistoryClass.Contact()

I have the feeling it's something I am overlooking but what?
I just need to add contacts to h

Thank you for your time.
Posted
Updated 28-Jan-19 4:37am

Using an object initializer:
VB.NET
Dim h As New HistoryClass.History With 
{
    .regarding = "test", 
    .startTime = Now, 
    .endTime = Now,
    .contacts =
    {
        New HistoryClass.Contact With { .id = ContactID }
    }
}
How to: Declare an Object by Using an Object Initializer (Visual Basic) | Microsoft Docs[^]

Using a separate property assignment with an array initializer:
VB.NET
h.contacts = 
{
    New HistoryClass.Contact With { .id = ContactID } 
}
How to: Initialize an Array Variable in Visual Basic | Microsoft Docs[^]

Using a list:
VB.NET
Dim contacts As List(Of HistoryClass.Contact)
contacts.Add(New HistoryClass.Contact With { .id = ContactID })
h.contacts = contacts.ToArray()
List<T>.ToArray Method (System.Collections.Generic) | Microsoft Docs[^]
 
Share this answer
 
 
Share this answer
 
sorry but that did not help.
I tried
h.contacts = New String() {"Small", "Medium", "Large"}

but the error is
Value of type 'String()' cannot be converted to 'HistoryClass.Contact()' because 'String' is not derived from 'HistoryClass.Contact'.

I am just a beginner.
 
Share this answer
 
Comments
Ralf Meier 28-Jan-19 9:27am    
Don't post a comment or information as answer ...

Your assignment doesn't work because contacts isn't an String-Array - it is a class-object.
So you should declare it inside your class 'History' as 'List (of Contact)'
Also your assignment to this Object must have either the sub-object 'id" and also the sub-object 'displayname'
Live for Coding 28-Jan-19 9:56am    
Thank you for your answer.
I have this in my class:
Public Property contacts As Contact()

What should I then change in this line?
h.contacts = New List(Of Contact) With {.id = ContactID}

Sorry for being so stupid or slow of understanding.
Richard Deeming 28-Jan-19 10:39am    
Just a beginner? You've been asking questions here since 2010, and you've been a member since September 2006!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900