Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
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…

739 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