如何动态添加元素到数组? [英] how to dynamically add elements to an array?

查看:295
本文介绍了如何动态添加元素到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在致电收到采购订单的申请服务。下面的代码工作(使用设置值),但是我将查询表以获取值,并且可以有任意数量的收据行。以下面的例子为例,查询结果如下:



LineId Amt Inc单位名称

00001 611.00 702.65 1 EACH

00002 327.50 376.62 1每个



如何循环查询结果并将每个数据添加到数组中?在此先感谢:)

Hi, I'm calling an application service to receipt purchase orders. The code below works (with set values), however I will be querying a table to get the values and there could be any number of receipt lines. Using the below as an example, the query results would look like this:

LineId Amt Inc Units Name
00001 611.00 702.65 1 EACH
00002 327.50 376.62 1 EACH

How would I loop through the query results and add each to the array? Thanks in advance :)

Dim loOrder_Receipt_Line1 As New Public.PurchaseOrder.Order_Receipt_Line

        With loOrder_Receipt_Line1
            .LineId = "00001"    
            .ReceivedForeignAmt = 611.00    
            .ReceivedForeignAmtInc = 702.65    
            .ReceivedUnits1 = 1    
            .ReceivedUnits1Name = "EACH"   
        End With

Dim loOrder_Receipt_Line2 As New Public.PurchaseOrder.Order_Receipt_Line

        With loOrder_Receipt_Line2
            .LineId = "00002"   
            .ReceivedForeignAmt = 327.50    
            .ReceivedForeignAmtInc = 376.62   
            .ReceivedUnits1 = 1    
            .ReceivedUnits1Name = "EACH"   
        End With

.Lines = New Public.PurchaseOrder.Order_Receipt_Line() { loOrder_Receipt_Line1, loOrder_Receipt_Line2 } 

推荐答案

数组不是为了添加任何内容而设计的向它致敬。数组基于连续初始化的内存区域。不要与方法 System.Array.Resize< T> 混淆。此方法不会调整任何对象的大小(因为它经常发生在Microsoft API,另一个令人困惑的名称),它实际上创建了一个全新的数组并复制旧数据。想象一下它的成本。阅读MSDN帮助页面上的 备注: https://开头MSDN。 microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx [ ^ ]。



最好不要使用这个函数。



相反,如果在初始化之前不确切知道数据大小,请不要使用数组。使用集合模板类之一,首先, System.Collections.Generic.List<>

https://msdn.microsoft.com/en-us/library/ 6sh2ey19%28v = vs.110%29.aspx [ ^ ]。



它的使用非常明显。我只想提醒您注意:在您填写列表中的数据并添加所需的所有元素后,如果需要,您可以暂时从此列表中创建一个数组: https://msdn.microsoft.com/en-us/library/x303t819(v = vs.110)的.aspx [ ^ ]。



-SA
Array is not designed to add anything to it. Arrays are based on continues region of memory initialized in one piece. Don't be confused with the method System.Array.Resize<T>. This method does not resize any objects (as it often happens to Microsoft API, another confusing name), it actually creates a brand new array and copies the data from the old one. Imagine the cost of it. Read "Remarks" on this MSDN help page: https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx[^].

Better don't use this function.

Instead, don't use array if you don't know the data size exactly before its initialization. Use one of collection template classes, first of all, System.Collections.Generic.List<>:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

Its use is quite obvious. I just want to bring to your attention: after you filled in the data in the list and added all elements you need, if you want, you can momentarily create an array out of this list: https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx[^].

—SA


这篇关于如何动态添加元素到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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