Brandon Brandon - 1 month ago 6
Vb.net Question

Vb.Net instantiate a Property of a class that is an array of a different class

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!

Answer

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.