Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. searching an index with FindIndex() method

searching an index with FindIndex() method

Scheduled Pinned Locked Moved C#
databasealgorithmshelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I've got a List object containing listviewitems. And i have a few items added to my listview control. With the findIndex() method i wanna search for an index of an element in the List object and add another element as a subItem to that element. The problem is, i don't understand the arguments FindIndex() expects. Can somebody give me an example? Thanks in advance!

    S H 2 Replies Last reply
    0
    • Y Yustme

      Hi, I've got a List object containing listviewitems. And i have a few items added to my listview control. With the findIndex() method i wanna search for an index of an element in the List object and add another element as a subItem to that element. The problem is, i don't understand the arguments FindIndex() expects. Can somebody give me an example? Thanks in advance!

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      MSDN topic for List.FindIndex Method[^]


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      • Y Yustme

        Hi, I've got a List object containing listviewitems. And i have a few items added to my listview control. With the findIndex() method i wanna search for an index of an element in the List object and add another element as a subItem to that element. The problem is, i don't understand the arguments FindIndex() expects. Can somebody give me an example? Thanks in advance!

        H Offline
        H Offline
        hpjchobbes
        wrote on last edited by
        #3

        The MSDN information is what I would read to get the information, as well as some sample code, though I do have some trouble understaing it sometimes. From my understanding (still learning) the FindIndex takes a predicate. The predicate is basically a function that accepts a value of whatever type your list contains, does a calculation, and returns true or false. So let's say I have a List of INTs. If I wanted to search for the first occurance of a number greater than 5, I would create a function like this: public bool IsGreaterThan5(int i) { if(i > 5) return true; else return false; } Then, when I want to find the first instance in my INT list that is greater than 5, I do this. int FirstOccurance = myIntList.FindIndex(IsGreaterThan5); There are also other variations of the FindIndex function that you can use: List.FindIndex(int32, predicate) - Limits the search range from the int32 element to the end of the list List.FindIndex(int32, int32, predicate) - Limits the search range from int32 to int32. Hope that helps!

        Y 1 Reply Last reply
        0
        • H hpjchobbes

          The MSDN information is what I would read to get the information, as well as some sample code, though I do have some trouble understaing it sometimes. From my understanding (still learning) the FindIndex takes a predicate. The predicate is basically a function that accepts a value of whatever type your list contains, does a calculation, and returns true or false. So let's say I have a List of INTs. If I wanted to search for the first occurance of a number greater than 5, I would create a function like this: public bool IsGreaterThan5(int i) { if(i > 5) return true; else return false; } Then, when I want to find the first instance in my INT list that is greater than 5, I do this. int FirstOccurance = myIntList.FindIndex(IsGreaterThan5); There are also other variations of the FindIndex function that you can use: List.FindIndex(int32, predicate) - Limits the search range from the int32 element to the end of the list List.FindIndex(int32, int32, predicate) - Limits the search range from int32 to int32. Hope that helps!

          Y Offline
          Y Offline
          Yustme
          wrote on last edited by
          #4

          Hi guys, I've read the msdn library for both code sample of this method and the documentation about it. But i didn't understand it very well. I've also searched with google for another example... This is what i've tried: // convertedString is an int int indexOfNumber = this.listViewItems.FindIndex(FindIndexOfListViewItem(convertedString); private int FindIndexOfListViewItem(int index) { for (int i = 0; i < this.listViewItems.Count; i++) { string currentItem = this.listViewItems[i].ToString(); //this.number is the item i wanna add as a subItem if (currentItem == this.number) { return i; } } return -1; } These are the errors i get: The best overloaded method match for 'System.Collections.Generic.List.FindIndex(System.Predicate)' has some invalid arguments Argument '1': cannot convert from 'int' to 'System.Predicate' What am i doing wrong? Thanks in advance! -- modified at 3:26 Thursday 11th January, 2007

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups