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

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.

Thanks
Sam
Samm1502Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sandsonCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Samm1502Author Commented:
Ahhh so obvious!  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.