Solved

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

Posted on 2014-02-06
4
268 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

776 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