简体   繁体   English

Vb.Net实例化一个类的属性,该类是另一个类的数组

[英]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 这是为了序列化和反序列化Xml对象

<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. 这对于反序列化可以正常工作,但是现在我试图正确创建此对象,以便可以将其序列化回XML对象。

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. 这里的最后一行不起作用,因为还没有实例化anotherTest.orderpart(0)。 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 我认为自己实例化它在正确的轨道上,就像我对'testOrderPart1'所做的那样,但是我不知道如何将其链接到我的anotherTest.orderpart

Please help! 请帮忙!

You have to initialize your array property first: 您必须先初始化您的array属性:

anotherTest.orderpart = New RequestMessageOrderaddOrderPart(10) {}

10 is array size. 10是数组大小。

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) . 但是,我认为您应该将属性声明更改为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. 当然,必须先使用Add方法插入该项目。

List(Of T) will work fine with serialization as well. List(Of T)也可以很好地与序列化一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM