TeDeSm
asked on
How to pass rows from List(of T) to a function
I am creating a List(of T) using a Reader.
After ensuring the List is sorted in a particular order I need to compare item values between rows and change an item value if required.
To do this I would like to pass two rows to a function for this comparison to take place. The Function is to be passed PreviousRow and NextRow and can return a Boolean or an integer. How do I pass the rows?
Whilst in the Function I will need to inspect item values from each row, how do I reference these items?
List is created from below code and sorted by the function further down:
After ensuring the List is sorted in a particular order I need to compare item values between rows and change an item value if required.
To do this I would like to pass two rows to a function for this comparison to take place. The Function is to be passed PreviousRow and NextRow and can return a Boolean or an integer. How do I pass the rows?
Whilst in the Function I will need to inspect item values from each row, how do I reference these items?
List is created from below code and sorted by the function further down:
If reader3.HasRows Then
Do While reader3.Read
'Create a new instance of the CustomerInvoicedItems object to place this iteration value
MyCustomerInvoicedItems = New CustomerInvoicedItems
'Add the data to the CustomerInvoicedItems object's properties
If String.IsNullOrEmpty(reader3("DespatchReceiptNumbers").ToString) Then
MyCustomerInvoicedItems.DespatchReceiptNumbers = lngDespatchNumber
Else
MyCustomerInvoicedItems.DespatchReceiptNumbers = CInt(reader3("DespatchReceiptNumbers"))
lngDespatchNumber = CInt(reader3("DespatchReceiptNumbers"))
End If
If Not IsDBNull(reader3("PrintSequenceNumber")) Then MyCustomerInvoicedItems.PrintSequenceNumber = CInt(reader3("PrintSequenceNumber"))
If Not IsDBNull(reader3("FloydDocumentNumber")) Then MyCustomerInvoicedItems.FloydDocumentNumber = CInt(reader3("FloydDocumentNumber"))
If Not IsDBNull(reader3("ItemCode")) Then MyCustomerInvoicedItems.ItemCode = reader3("ItemCode").ToString
If Not IsDBNull(reader3("ItemDescription")) Then MyCustomerInvoicedItems.ItemDescription = reader3("ItemDescription").ToString
If Not IsDBNull(reader3("LineQuantity")) Then
MyCustomerInvoicedItems.LineQuantity = CInt(reader3("LineQuantity"))
Else
MyCustomerInvoicedItems.LineQuantity = 0
End If
If Not IsDBNull(reader3("UnitSellingPrice")) Then
MyCustomerInvoicedItems.UnitSellingPrice = CDbl(reader3("UnitSellingPrice").ToString())
Else
MyCustomerInvoicedItems.UnitSellingPrice = 0
End If
If Not IsDBNull(reader3("UnitDiscountPercent")) Then
MyCustomerInvoicedItems.UnitDiscountPercent = CDbl(reader3("UnitDiscountPercent").ToString())
Else
MyCustomerInvoicedItems.UnitDiscountPercent = 0
End If
If Not IsDBNull(reader3("LineTypeID")) Then MyCustomerInvoicedItems.LineTypeID = CInt(reader3("LineTypeID"))
If Not IsDBNull(reader3("SOPInvoiceCreditLineID")) Then
MyCustomerInvoicedItems.SOPInvoiceCreditLineID = CInt(reader3("SOPInvoiceCreditLineID"))
Else
MyCustomerInvoicedItems.SOPInvoiceCreditLineID = 0
End If
'Add the single object instance created above to the List (of InvoiceMainDetail) objects
MyCustomerInvoicedItemsList.Add(MyCustomerInvoicedItems)
Loop
MyCustomerInvoicedItemsList = MyCustomerInvoicedItemsList.OrderBy(Function(x) x.SOPInvoiceCreditLineID).ToList()
ASKER
Thanks Paul, I see that the rows are referenced using the index MyFunction (MyCustomerInvoicedItemsLi st.items(2), MyCustomerInvoicedItemsLis t.items(3) ) . 2 and 3 in your example, is there a way to get the index without counting through the list?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Paul, spot on with your help.
Open in new window
and to call it :
Debug.print MyFunction (MyCustomerInvoicedItemsLi