Solved

Collection C#

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

12 Experts available now in Live!

Get 1:1 Help Now