Solved

Collection C#

Posted on 2014-09-11
2
103 Views
Last Modified: 2014-09-18
trying to understand when I do call public Person this[int index] {get {
                return (Person)List[index];
  I'm just a little new trying to understand this concept..

class PersonCollection : CollectionBase
    {
        public void Add(Person person) {
            List.Add(person);

            }

        public void Insert(int index, Person person) {
            List.Insert(index, person);
        
        }

        public void Remove(Person person) {
            List.Remove(person);
            }

        public Person this[int index] {

            get {
                return (Person)List[index];
            
            }
            set
            {
                List[index] = value;
            
                }
        
            }    
    }

Open in new window


PersonCollection person = new PersonCollection();
            person.Add(new Person() {
            PersonID = 1,
            Name = "Yves",
            LName ="Guyon"
            
                });

            person.Add(new Person()
            {
                PersonID = 2,
                Name = "Steve",
                LName = "Smith"

            });

            person.Add(new Person()
            {
                PersonID = 3,
                Name = "Mark",
                LName = "Guyon"

            });

            person.Add(new Person()
            {
                PersonID = 3,
                Name = "Mark",
                LName = "Guyon"

            });

Open in new window

0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40318057
Assuming I understand what you are asking, you would access the indexer in this manner:

PersonCollection person = new PersonCollection();

...

Person p = person[0];

Open in new window


You can do this because you defined your indexer to accept integers as parameters.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40318618
the this[ ] construction converts your class into an indexable one.
It is expected that you reference an internal collection inside that class, in order to get the real data and return it in the 'get' block.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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