0

This is for the purpose of Serializing and Deserializing an Xml object

<XmlRoot("orderadd")>
<Serializable()> _
Public Class clsSMsgRequestMessage
    <XmlElementAttribute()> Public Property ordertype() As String
    <XmlElementAttribute()> Public Property vehicleid() As String
    <XmlElementAttribute()> Public Property orderpriority() As String
    <XmlElementAttribute("orderpart")> Public Property orderpart() As RequestMessageOrderaddOrderpart()
    <XmlAttributeAttribute()> Public Property clientid() As String
    <XmlAttributeAttribute()> Public Property transactionid() As String
    <XmlAttributeAttribute()> Public Property numberoforderparts() As String

    Public Sub New()
    End Sub
End Class

Public Class RequestMessageOrderaddOrderpart
    <XmlElementAttribute()> Public Property operation() As String
    <XmlElementAttribute()> Public Property location() As String
    <XmlElementAttribute()> Public Property loadtype() As String
    <XmlAttributeAttribute()> Public Property orderpartnumber() As String

    Public Sub New()
    End Sub

End Class

This works correctly for deserializing, but now I am trying to create this object correctly so I can serialize it back to an XML object.

Dim anotherTest As clsSMsgRequestMessage = New clsSMsgRequestMessage()
Dim testOrderPart1 As New RequestMessageOrderaddOrderpart


anotherTest.clientid = "data"
anotherTest.orderpriority = "data"
anotherTest.ordertype = "data"
anotherTest.transactionid = "data"
anotherTest.vehicleid = "data"
anotherTest.numberoforderparts = "data"

testOrderPart1.loadtype = "data"
testOrderPart1.location = "data"
testOrderPart1.operation = "data"
testOrderPart1.orderpartnumber = "data"


anotherTest.orderpart(0) = testOrderPart1

The last line here doesn't work because anotherTest.orderpart(0) isn't instantiated yet. But I don't know how to instantiate it because

anotherTest.orderpart(0) = New RequestMessageOrderaddOrderPart

still comes back with a "Object reference not set to an instance of an object."

anotherTest.orderpart = New RequestMessageOrderaddOrderPart

comes back with "Value of type cannot be converted to '1-dimensional array of "

I think I'm on the right track with instantiating it by itself like I'm doing with 'testOrderPart1' but I don't know how to link that to my anotherTest.orderpart

Please help!

1 Answer 1

2

You have to initialize your array property first:

anotherTest.orderpart = New RequestMessageOrderaddOrderPart(10) {}

10 is array size.

After that you'll be able to set first array element:

anotherTest.orderpart(0) = New RequestMessageOrderaddOrderPart

Update

However, I think you should change your property declaration to become List(Of RequestMessageOrderaddOrderPart). With a list you don't have to specify number of items:

anotherTest.orderpart = New List(Of RequestMessageOrderaddOrderPart)()

Adding items is really easy:

anotherTest.orderpart.Add(new RequestMessageOrderaddOrderPart())

And you're still able to get/modify items using indexers:

Dim firstItem = anotherTest.orderpart(0)

Of course, that item has to be inserted using Add method first.

List(Of T) will work fine with serialization as well.

Sign up to request clarification or add additional context in comments.

2 Comments

oh cool thanks! Do I have to set it to a fixed size like that? basically the XML message will have a variable number of 'parts' coming back and I won't know. I suppose they'll never have more than 20 or so parts - would be okay to just initialize a huge array and then regularly not use most of them?
I highly suggest using a List(Of RequestMessageOrderaddOrderpart) instead of an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.