Solved

Collection C#

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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