locked
Find List(of T) RRS feed

  • Question

  • I looked at the some few example of how to implement/use the 'Find' method, but I'm still not sure on the code logic

    dim list as List(of Product)
    dim listType as List(of ProductType)

    Now. I want to find something from the list that with producttype.

    listType.find(...??) What I can pass in?  I actually want to find something that refer to the list. What is the perdicted? I am not sure how to use this...confused ...

    Thanks
    Friday, July 24, 2009 3:49 AM

Answers

  • You make a function that returns the object of the class Product if true

    dim foundProduct as Product = listType.Find(Address of theFunctionName)


    Success
    Cor
    Friday, July 24, 2009 6:27 AM
  • I think Find was really invented to support LINQ. While you can use is as Cor suggests, or you can use a lambda function, it's easier to use LINQ. Here's an example:

            Dim results = From prod In lst Where prod.ProductType = "Whatever"
            For Each result In results
                MessageBox.Show(result.ProductName)
            Next

    Rod

    Rod Stephens, Visual Basic MVP

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Friday, July 24, 2009 12:49 PM
  • A lot depends on what you're actually trying to do.  Here's a simple example of trying to find a ProductType by the product name:

    Module Module1
    
    Sub Main()
    
    	Dim listProductType As List(Of ProductType) = New List(Of ProductType)
    	listProductType.Add(New ProductType("Exciting New Product"))
    
    	Dim pt As ProductType = FindProduct(listProductType, "Exciting New Product")
    
    End Sub
    
    Public Function FindProduct(ByVal Products As List(Of ProductType), ByVal ProductName As String) As ProductType
    	For Each pt As ProductType In Products
    		If pt.ProductName = ProductName Then Return pt
    	Next
    
    	Return Nothing
    End Function
    
    End Module
    
    
    Public Class ProductType
    	Public ProductName As String
    
    Public Sub New(ByVal PN As String)
    	ProductName = PN
    End Sub
    
    End Class
    Monday, July 27, 2009 2:01 PM

All replies

  • You make a function that returns the object of the class Product if true

    dim foundProduct as Product = listType.Find(Address of theFunctionName)


    Success
    Cor
    Friday, July 24, 2009 6:27 AM
  • I think Find was really invented to support LINQ. While you can use is as Cor suggests, or you can use a lambda function, it's easier to use LINQ. Here's an example:

            Dim results = From prod In lst Where prod.ProductType = "Whatever"
            For Each result In results
                MessageBox.Show(result.ProductName)
            Next

    Rod

    Rod Stephens, Visual Basic MVP

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Friday, July 24, 2009 12:49 PM
  • Find has existed on List(Of T) long before LINQ...


    Tom Shelton
    Friday, July 24, 2009 5:34 PM
  • I stand corrected.

    Rod

    Rod Stephens, Visual Basic MVP

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Friday, July 24, 2009 5:41 PM
  • A lot depends on what you're actually trying to do.  Here's a simple example of trying to find a ProductType by the product name:

    Module Module1
    
    Sub Main()
    
    	Dim listProductType As List(Of ProductType) = New List(Of ProductType)
    	listProductType.Add(New ProductType("Exciting New Product"))
    
    	Dim pt As ProductType = FindProduct(listProductType, "Exciting New Product")
    
    End Sub
    
    Public Function FindProduct(ByVal Products As List(Of ProductType), ByVal ProductName As String) As ProductType
    	For Each pt As ProductType In Products
    		If pt.ProductName = ProductName Then Return pt
    	Next
    
    	Return Nothing
    End Function
    
    End Module
    
    
    Public Class ProductType
    	Public ProductName As String
    
    Public Sub New(ByVal PN As String)
    	ProductName = PN
    End Sub
    
    End Class
    Monday, July 27, 2009 2:01 PM