How to access members of my list by name instead of index

Posted on 2008-11-05
Last Modified: 2013-11-07
Hi there

I have a C# class that looks like the following:

namespace MyNameSpace.Data.Objects
    public class LevelsList : CollectionBase
        public LevelListItem this[int index]
              get { return ((LevelListItem)(List[index])); }
              set { List[index] = value; }

       public int Add(LevelListItem levelListItem)
            return List.Add(levelListItem);


My LevelListItem class looks like this:

    public class LevelListItem
      private int level;
      private string description;
      private bool selected;

      public int Level
          get { return this.level; }
          set { this.level = value; }


      public string Description
          get { return this.description; }
          set { this.description = value; }


      public bool Selected
          get { return this.selected; }
          set { this.selected = value; }

What I want to be able to do is to access the correct LevelListItem from my LevelsList using the
Description property of LevelListItem rather than its index position in the list.  So something like this:

        public LevelListItem Find(string description)
            foreach LevelListItem li in List
                if(li.Descrption == description)
                    return (LevelListItem) li;
   However this gives me 2 syntax errors - one hightlights LevelListItem after foreach and the second says it is expecting a ) after the List word??

Anybody any ideas.

Question by:Samm1502
    LVL 2

    Accepted Solution

    Hi Sam,

    this is because you haven't used the correct syntax for the foreach instruction. Here is the correct syntax
           public LevelListItem Find(string description)
                foreach(LevelListItem li in List)
                    if(li.Description == description)
                        return li;

    Open in new window


    Author Closing Comment

    Ahhh so obvious!  Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now