我有这个代码,使用vb.net将数据插入两个不同的数组:
salesman_product(salesman_product_counter) = reader2.GetString(0)
salesman_product_amount(salesman_product_counter) = "£" + Val(reader2.GetString(1)).ToString("F2")
salesman_product_counter
是一个每次都有+1的数字(数组在while循环中)
创建数组工作正常,我如何遍历两个数组以获取数据?
我试过了:
For Each salesman_product2 In salesman_product and salesman_product_amount2 in salesman_product_amount
Salesman_xlWorkSheet.Cells(salesman_product2, 1) = company_name 'client name
Salesman_xlWorkSheet.Cells(salesman_product2, 2) = customer_account 'client account
Salesman_xlWorkSheet.Cells(salesman_product2, 3) = salesman_product2 'product
Salesman_xlWorkSheet.Cells(salesman_product2, 4) = reader4.GetString(0) 'commission percentage
Salesman_xlWorkSheet.Cells(salesman_product2, 5) = Lines_International_Calls * (reader4.GetString(0) / 100) 'commission amount
Next
但你不能使用AND
我需要能够在循环中看到来自两个数组的数据
答案 0 :(得分:2)
你必须循环索引。
For index As Integer = 0 To salesman_product.Count-1
salesman_product2 = salesman_product(index)
salesman_product_amount2 = salesman_product_amount(index)
Salesman_xlWorkSheet.Cells(salesman_product2, 1) = company_name 'client name
Salesman_xlWorkSheet.Cells(salesman_product2, 2) = customer_account 'client account
Salesman_xlWorkSheet.Cells(salesman_product2, 3) = salesman_product2 'product
Salesman_xlWorkSheet.Cells(salesman_product2, 4) = reader4.GetString(0) 'commission percentage
Salesman_xlWorkSheet.Cells(salesman_product2, 5) = Lines_International_Calls * (reader4.GetString(0) / 100) 'commission amount
Next
但这样做的正确方法不是创建2个数组,而是创建类的列表
您需要为销售员信息创建一个类
Class SalesmanProduct
Public Property Name As String
Public Property Amount As String
End Class
将产品添加到列表中
Dim salesmanProducts As New List(Of SalesmanProduct)
' ...
Dim newProduct As New SalesmanProduct
newProduct.Name = reader2.GetString(0)
newProduct.Amount = "£" + Val(reader2.GetString(1))
salesmanProducts.Add(newProduct)
然后你可以" For Each"列表
For Each product As SalesmanProduct In salesmanProducts
' product.Name
' product.Amount
Next
我可以说在数据库中将数量存储为字符串是一个坏主意,但这与此问题无关。