locked
How do you find an item in a list & then delete it? RRS feed

  • Question

  • User695372294 posted

    I am a VB.NET newbie and unfamiliar with the use of lists. If I define a list as follows, how do I find a specific item (such as "Parrot") and how would I then delete this item from the list? 

    Dim list As New List(Of String)
    
            list.Add("Dog")
            list.Add("Rabbit")
            list.Add("Parrot")
            list.Add("Cat")
    
     
    Saturday, September 20, 2008 4:41 AM

All replies

  • User-417784260 posted

     

    The generic list has a find method that you pass in a function which return true for a match

            Dim list As New List(Of String)

            list.Add("Dog")
            list.Add("Rabbit")
            list.Add("Parrot")
            list.Add("Cat")
            what = "Parrot"

            Dim parrot As String = list.Find(AddressOf FindMe)

            list.Remove(parrot)
        End Sub

        Public Function FindMe(ByVal name As String) As Boolean
            Return name = what
        End Function

     

    Saturday, September 20, 2008 9:17 AM
  • User695372294 posted

    Hi Ken,
    Thanks for that. Your solution is certainly easy enough to copy blindly into my code. However it's anything but easy for a newbie like me to understand how it works. I'm struggling to understand how the FindMe function is used, mainly because I am unfamiliar with the use of AddressOf. I looked up AddressOf in the Visual Web Developer Express 2008 online help and got the following explanation:

    The AddressOf operator creates a function delegate that points to the function specified by procedurename. When the specified procedure is an instance method then the function delegate refers to both the instance and the method. Then, when the function delegate is invoked the specified method of the specified instance is called.

    That is a typical example of 'help' that is written for those who don't need it!!! For anyone new to the subject it is mostly gobbledook. When I did VBA programming with Access I used to find the online help genuinely useful and written in relatively plain English. For VB.NET, the use of plain English seems to be forbidden.

    BTW, is there any way in Visual Web Developer Express 2008 to run little code snippets like this for test purposes without going to the trouble of creating a web page with a button that triggers the code? 

    David

    21 Sep 08: Edited to improve clarity

    Saturday, September 20, 2008 11:55 AM
  • User-990694832 posted

    As an alternative, Use LINQ.

    dim result = (from item in yourlist where item = desired_value select item).firstordefault() 

    For testing investigate TDD

    http://www.testdriven.net/ 

     

    Hope this helps

    DK              

    Monday, September 22, 2008 8:32 AM
  • User695372294 posted

    Hi DK,
    That's an interesting idea. I only started with ASP.NET a few months ago and made the decision to avoid learning ADO.NET in favour of Linq to SQL (which I now understand enough to do some basic database CRUD actions). However, I've not yet pursued the other flavours of Linq. Which version of Linq does your solution exploit? 

    The TestDriven product would probably do what I was looking for but I'm not sure that it would save me enough time to justify its cost. I'm coding for a non-profit organisation and funds are tight. However, your answer at least confirms that I'm not missing a basic facility in VWD!

    David 

    Monday, September 22, 2008 9:26 AM
  • User-990694832 posted

    well Linq is linq but in this case the acronym is LINQ to Objects - works the same as LINQ-sql

    I use mbUntit which is a free TDD framework, which TestDriven adds onto or uses.
    Here is a better link
    http://www.mbunit.com/

    If there are any mbunit questions create another forum post and i or some body else will help
    Hope this helps
    DK

    Monday, September 22, 2008 12:18 PM
  • User695372294 posted

    As you say, Linq is Linq. From the beginning, I liked the concept that you could use similar processes for handling widely different data sources, though up to now I've never had the need to use anything other than Linq to SQL.

    I've now installed MbUnit 2.4 but have still to read the documentation and learn how to use it. Thanks for bringing it to my attention.

    David

    Tuesday, September 23, 2008 4:40 AM