如何将对象添加到对象?

时间:2013-02-14 18:16:09

标签: .net vb.net soap multidimensional-array

我对在5台服务器上运行的同一服务进行相同的SOAP调用。要返回的所有TWeeksHeld个集合需要连接在一起,形成同一对象(TDAEHeldList)的输出版本。这是我的课程:

Partial Public Class TDAEHeldList      
    Private siteIdField As String        
    Private countField As Integer        
    Private returnCodeField As String        
    Private errorMessageField As String        
    Private daeWeeksHeldField() As TWeeksHeld
End Class

Partial Public Class TWeeksHeld        
    Private checkInField As String        
    Private resortNameField As String        
    Private countryField As String        
    Private regionField As String        
    Private localityField As String        
    Private weekIdField As String        
    Private bedroomsField As String        
    Private sleepsField As String        
    Private bookingTypeField As String        
    Private resortIdField As String        
    Private timeToExpiryField As String        
    Private specialFlagField As String        
    Private extMemberNoField As String        
    Private stockDisplayField As String        
    Private ownerBusCatCodeField As String
End Class

每次调用都需要将daeWeeksHeldField()集合的内容添加到输出类“daeWeeksHeldField()集合中。”

我有以下工作:

Dim returnData As New TDAEHeldList
Dim weeksOnHold As New TDAEHeldList
Dim weeksOnHoldArray As TWeeksHeld
weeksOnHold = WSCaller.GetForeignWeeksOnHold(siteID,affiliateMemberID)

然后尝试添加

weeksOnHoldArray += String.Concat(weeksOnHoldArray, weeksOnHold.daeWeeksHeldField)

其次是:

returnData.daeWeeksHeldField = weeksOnHoldArray.ToArray(GetType(TWeeksHeld))

我也试过了:

for Each x As Object In weeksOnHold.daeWeeksHeld
  weeksOnHoldArray.Add(x)
Next

加上其他几种Google搜索技巧。我想做什么或不做,如果是,它是如何完成的?

1 个答案:

答案 0 :(得分:0)

您将daeWeeksHeldField属性称为集合,但它不是集合。这是一个数组。数组肯定有它们的用途,但在这种情况下,你真正想要的是一个集合。集合允许您轻松添加新项目。另一方面,数组不能自动动态增长,因此如果列表的大小不固定,它们会变得很麻烦。

我使用的最常见的集合是List(Of T)。以下是您将如何使用它:

Dim combinedWeeksOnHold As New List(Of TWeeksHeld)
Dim weeksOnHold1 = WSCaller1.GetForeignWeeksOnHold(siteID1, affiliateMemberID1)
If weeksOnHold1.daeWeeksHeldField IsNot Nothing Then
    combinedWeeksOnHold.AddRange(weeksOnHold1.daeWeeksHeldField)
End If
Dim weeksOnHold2 = WSCaller2.GetForeignWeeksOnHold(siteID2, affiliateMemberID2)
If weeksOnHold2.daeWeeksHeldField IsNot Nothing Then
    combinedWeeksOnHold.AddRange(weeksOnHold2.daeWeeksHeldField)
End If

如果你真的需要它们的数组,你可以随时拨打List(Of T).ToArray,如下所示:

Dim array() As TWeeksHeld = combinedWeeksOnHold.ToArray()