如何在数组索引中添加项目 [英] How to add item in array index

查看:71
本文介绍了如何在数组索引中添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码

Below is my code

Dim OrderItemsQty As New V2.orderItemIdQty

 OrderItemsQty.order_item_id = "545"
 OrderItemsQty.qty = 1

 Dim OrderItemsQty1 As New V2.orderItemIdQty
 OrderItemsQty1.order_item_id = "SHI054-Black-7"
 OrderItemsQty1.qty = 1

Dim ItemsQty() As V2.orderItemIdQty = {OrderItemsQty, OrderItemsQty1}



上面我已添加OrderItemsQty和OrderQty

现在我想要它在循环中动态添加多个orderitemsQty。 。


Above I have add OrderItemsQty and OrderQty
now i am want to it dynamically add multiple orderitemsQty in loop..

推荐答案

您应该考虑使用列表< t>< / t> 而不是数组。阅读此MSDN页面中的集合作为数组的替代部分:Visual Basic中的数组 [ ^ ]。
You should consider using a List<t></t> instead of an array. read the "Collections as an Alternative to Arrays" section in this MSDN page: "Arrays in Visual Basic"[^].


上面建议的解决方案是优先考虑的解决方案。但仍然如果你想继续使用数组。

然后你可以使用下面的代码:



For(i as Integer = 1 To 10)

'调整阵列大小并保留其内容

Redim Preserve ItemsQty(ItemsQty.Length + 1)

'添加新项目到数组结束

ItemsQty(ItemsQty.Length - 1)= new V2.orderItemIdQty

'对你的新对象做任何其他操作

End For
The solution suggested above is a preffered one. But still if you want to continue using Array.
Then you can use the following code:

For(i as Integer = 1 To 10)
'Resize your array and preserve its contents
Redim Preserve ItemsQty(ItemsQty.Length + 1)
'Add the new item to the end of Array
ItemsQty(ItemsQty.Length - 1) = new V2.orderItemIdQty
'Do any other peice of operation on your new object
End For


Dim list As New List(Of CMagentoOrder)
           list = salesOrderInfo(OrderIncrementedID)
           Dim Items(list.Count) As V2.orderItemIdQty
           Dim obj As New CMagentoOrder
           Dim i = 0
           For i = 0 To list.Count - 1
               For Each item As CMagentoOrder In list
                   Dim qty As New V2.orderItemIdQty
                   qty.order_item_id = item.ItemsQty_Order_item_id
                   qty.qty = item.ItemsQty_Qty
                   Items(i) = qty
                   i = i + 1
               Next
           Next


这篇关于如何在数组索引中添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆