Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-06
4
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

636 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