Solved

Collection C#

Posted on 2014-09-11
2
99 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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