I need to format an input data structure for an RPG web service. I've tried using a list and it does not work. When I try this code:
Dim clist As New List(Of LabelView.PASSBACK.dscustomers)
clist.Add(New LabelView.PASSBACK.dscustomers() With {.BALANCEDUE = 185.42, .CREATIONDATE = 20200101, .CUSTOMERID = 1, .CUSTOMERTYPE = "ACTIVE", .FIRSTNAME = "Pat", .LASTNAME = "Smith"})
clist.Add(New LabelView.PASSBACK.dscustomers() With {.BALANCEDUE = 185.42, .CREATIONDATE = 20200101, .CUSTOMERID = 2, .CUSTOMERTYPE = "ACTIVE", .FIRSTNAME = "Jordan", .LASTNAME = "Jones"})
input.DSCUSTOMERS = clist
// ^^ Intellisense error:
// Value of type 'List (Of dsCustomers)' cannot be converted to 'dscustomers()'.
I can't figure out how to programmatically build something compatible. When I try this:
Dim client As PASSBACK.PASSBACK = New PASSBACK.PASSBACK()
Dim input As PASSBACK.passbackInput = New PASSBACK.passbackInput
input.DSCUSTOMERS(0).FIRSTNAME = "Hello"
It compiles but when I run I get 'Object reference not set to an instance of an object.'
Your issue with this line:
input.DSCUSTOMERS = clist
Is because DCUSTOMERS
is an array (the "dcustomers()" of your error message) and a List(Of dcustomer)
can't be directly assigned to an array.
There is a conversion function on List(Of T)
that will give you an array T()
called ToArray
. It requires only a small change to your code:
input.DSCUSTOMERS = clist.ToArray()
This will make a new array with the same contents as the List
(if dcustomers
is a Class
then the contents of the array will be exactly the same objects, whereas if it's a Structure
then they will be copies) and assign it to DCUSTOMERS
.