Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now