Solved

How to pass rows from List(of T) to a function

Posted on 2014-02-06
4
269 Views
Last Modified: 2014-02-06
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:

			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

Open in new window


MyCustomerInvoicedItemsList = MyCustomerInvoicedItemsList.OrderBy(Function(x) x.SOPInvoiceCreditLineID).ToList()

Open in new window

0
Comment
Question by:TeDeSm
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 39838541
Something like:

Public Function  MyFunction  (Byval previousRow as CustomerInvoicedItems,  Byval nextRow as CustomerInvoicedItems) as Boolean

     if previousRow.UnitDiscountPercent  <> nextRow.UnitDiscountPercent then
           ' do something
     end if

End Function

Open in new window


and to call it :

Debug.print MyFunction (MyCustomerInvoicedItemsList.items(2),  MyCustomerInvoicedItemsList.items(3) )
0
 

Author Comment

by:TeDeSm
ID: 39838555
Thanks Paul, I see that the rows are referenced using the index MyFunction (MyCustomerInvoicedItemsList.items(2),  MyCustomerInvoicedItemsList.items(3) ) . 2 and 3 in your example, is there a way to get the index without counting through the list?
0
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 500 total points
ID: 39838574
If you only wanted to pass one item you could use:
For each item as CustomerInvoicedItems in MyCustomerInvoicedItemsList
     Call SomeFunction(item)
Next

Since you want two items you need to have the index so:
for ix as integer =0 to  MyCustomerInvoicedItems.Count-2
     Debug.print  MyFunction (MyCustomerInvoicedItems(ix), MyCustomerInvoicedItems(ix+1) )
Next

You could use IndexOf to find the index of a particular item that you already hold a reference to.

e.g.  ixCurrent = MyCustomerInvoicedItems.IndexOf(currentCustomerInvoicedItem)
0
 

Author Closing Comment

by:TeDeSm
ID: 39838592
Thanks Paul, spot on with your help.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
write xml in vb.net 2 34
Sql server, import complete table, using vb.net 9 43
Iteration Help (Asp.net VB) 5 24
Open a link in vb.net 2 16
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question