如何添加到此阵列?

时间:2014-05-05 15:05:35

标签: vb.net winforms

我有一个名为pushOrderIncQTY的对象。我可以使用我有一个接受多个项目的重载将一个项目插入到数组中。我的问题是:如何调整数组大小并为其添加记录?

Dim pushOrderIncQTY() As infoPushOrderIncQTY

With pushOrderIncQTY
    .costPrice = thisentry.Item("costPrice")
    .externalTimeStamp = DateTime.Now()
    .RootPLU = thisentry.Item("tagbarcode") 'set this to the barcode from the file
    .sizeBit = -666
    .supplierID = cfb.SupplierID
    .orderReference = thisentry.Item("OrderNumber")
    .orderLineReference = ""
    .externalTransaction = ""
    .sourceShop = cfb.SiteId 'set to the GEMINI location ID for this store (you will have to get this from your configuration file
    .destinationShop = cfb.SiteId 'set this to the same as the sourceshop
    .QTY = thisentry.Item("ActQty")
    .whichQty = LiveSales.infoPushOrderIncQTY.Which_OrderQty.delivered 'only available option at present
End With

在这里编辑 好的,我可以使用列表,我测试了这个代码,并且它把16个记录很好但是如何让它分批进行

    Dim pushOrderInQty As New List(Of infoPushOrderIncQTY)()

    For Each thisentry2 In orderLineData.Rows
        With pushOrderIncQTY

            .costPrice = thisentry2.Item("costPrice")
            .externalTimeStamp = DateTime.Now()
            .RootPLU = thisentry2.Item("tagbarcode") 'set this to the barcode from the file
            .sizeBit = -666
            .supplierID = cfb.SupplierID
            .orderReference = thisentry2.Item("OrderNumber")
            .orderLineReference = ""
            .externalTransaction = ""
            .sourceShop = cfb.SiteId 'set to the GEMINI location ID for this store (you will have to get this from your configuration file
            .destinationShop = cfb.SiteId 'set this to the same as the sourceshop
            .QTY = thisentry2.Item("ActQty")
            .whichQty = LiveSales.infoPushOrderIncQTY.Which_OrderQty.delivered 'only available option at present

        End With
        recordCount = recordCount + 1

        pushOrderInQty.Add(pushOrderIncQTY)
    Next

    CallWebSerivce(wrpPush, request, pushOrderInQty.ToArray())

我想要的是能够在cfb.batchsize中设置批量大小,cfb.batchsize是我的配置文件的包装器,我想要的是如果它们是20条记录且批量大小为5则表示只应调用Web服务4次而不是20次?并且在记录集完成之前,添加到列表的记录只有5个?

2 个答案:

答案 0 :(得分:2)

您可以使用ReDim调整任何arry的大小,但它会擦除内容。 ReDim Preserve将保留内容。

ReDim Preserve pushOrderIncQTY(5)

'or just make it one element larger, which is something you DO NOT want to do in a loop

Dim oldSize as integer = UBound(pushOrderIncQTY) 
ReDim Preserve pushOrderIncQTY(oldSize + 1)

顺便说一句,您也可以将其定义为类型List(pushOrderIncQTY),然后在加载列表后,您可以使用.ToArray()方法将其转换为数组。

答案 1 :(得分:2)

你问过两个问题。所以这是对你的第二个问题的单独回答。您只需要在循环中计算代表。当您点击批量大小时,请调用您的Web服务并清空列表(重新开始使用新批次)。像这样:

Dim pushOrderInQty As New List(Of infoPushOrderIncQTY)()

For Each thisentry2 In orderLineData.Rows
    With pushOrderIncQTY
        .costPrice = thisentry2.Item("costPrice")
        .externalTimeStamp = DateTime.Now()
        '...etc
    End With
    recordCount = recordCount + 1

    pushOrderInQty.Add(pushOrderIncQTY)

    If recordCount >= cfb.BatchSize Then
        CallWebSerivce(wrpPush, request, pushOrderInQty.ToArray())
        pushOrderInQty.Clear()
        recordCount = 0
    End If
Next

'get the last, partial batch
If pushOrderInQty.Count > 0 Then
    CallWebSerivce(wrpPush, request, pushOrderInQty.ToArray())
End If