5 Replies - 17185 Views - Last Post: 22 September 2010 - 09:51 PM Rate Topic: -----

#1 Bixel  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 20-August 10

Get index of item in list<>

Posted 22 September 2010 - 07:21 PM

I got a List of Labels

List<Label> invWeaponEquipCount;

I need to check if it has a certain label - but I only have a string..
     Label element; 

     if (element == invWeaponEquipCount.Find(delegate(Label s) { return s.Text.Contains(oldWeapon.Name); }))
     {
         int index;

     }



I need to get the index of that Label inside invWeaponEquipCount

I was trying to use IndexOf, but couldn't get it to work.. is that the right approach?

Is This A Good Question/Topic? 0
  • +

Replies To: Get index of item in list<>

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Get index of item in list<>

Posted 22 September 2010 - 07:40 PM

Maybe List<(Of <(T>)>).FindIndex Method (Predicate<(Of <(T>)>))?
Was This Post Helpful? 2
  • +
  • -

#3 Bixel  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 20-August 10

Re: Get index of item in list<>

Posted 22 September 2010 - 08:02 PM

Awesome!! that pointed me in the right direction and made the code much better -
I created an anonymous delegate
        
private int FindWeaponIndex(string name)
    {
       return invWeaponEquipCount.FindIndex(delegate(Label label) { return label.Text == name; });
    }


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: Get index of item in list<>

Posted 22 September 2010 - 08:32 PM

May I suggest you REALLY rethink your program archetecture?

A List<> of Labels? Really?

Using a label to store your values is just really, really ill-advised.

Its more common to make a property, and when you change the value, it updates the label. It sounds like you are doing it the other way around. Maybe making a new label for every new item? What happens if you runout of screen space? Or want to redesign the GUI? If your property knows what control to update then you only have to make one change in one place.

private List<string> _weapons = new List<string>();
public List<string> Weapons
{
   get { return _weapons; }
   set { 
           _weapons = value;
           myListBox.Items.Clear();
           myListBox.Items.AddRange(value);
}

Was This Post Helpful? 1
  • +
  • -

#5 Bixel  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 20-August 10

Re: Get index of item in list<>

Posted 22 September 2010 - 08:57 PM

I am not using Forms, and Labels is not Forms.Labels
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Get index of item in list<>

Posted 22 September 2010 - 09:51 PM

Two notes. If I were architecting this, I probably wouldn't get the index of the proper Label, I'd return the object itself. You know more about your architecture than I do, but that's just a suggestion. That way, if you change your collection to a Dictionary or something later, you don't have to worry about that breaking.

Second, have you heard about Lambdas? I wrote a tutorial about this, in case you're unfamiliar with them.

They're basically a shorthand syntax for anonymous delegates. To me, they look cleaner and are easier to read and identify what's going on.

Your code could be rewritten as follows:

return invWeaponEquipCount.FindIndex(label => label.Text == name);


Or should you go the route of returning the object itself:

return invWeaponEquipCount.SingleOrDefault(label => label.Text == name);


SingleOrDefault returns either one match, or null. If there are multiple matches, an Exception is thrown. Single would return one match, and throw an Exception if there is either zero or multiple matches. First would return the first match, or null if there are none.
Was This Post Helpful? 4
  • +
  • -

Page 1 of 1