Indexer question...

This is a discussion on Indexer question... within the C# Programming forums, part of the General Programming Boards category; Is there a way to implement an Indexer on a class member and not the class itself? This is strictly ...

  1. #1
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323

    Indexer question...

    Is there a way to implement an Indexer on a class member and not the class itself?

    This is strictly for code readability because I would like the idea of having an Indexer property of a variable within a class.

    An Example would be

    Code:
        public class MyClass
        {
            private List<PlayerClass> player = new List<PlayerClass>();
    
            public PlayerClass Player[int index]
            {
                get { return player[index]; }
                set { player.Add(value); }
            }    
        }
    Then lets say I instantiate a MyClass object

    MyClass blah = new MyClass();

    I would be able to access the player list like so...

    blah.Player[1]

    without making the player list public. I would like to keep this member private so that I can use the properties to validate the input (even though that isn't shown in this example). I know I can do this with a method and keep my list private, I was just hoping i could use the indexer notation []. Any ideas?
    The keyboard is the standard device used to cause computer errors!

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You can do it with a secondary helper class.

    E.g. MyClass.Player is a normal property that returns an object of another class called something like PlayerCollection (you could also make this class implement IEnumerable so that you can use it inside foreach) and then just have this second class support an indexer method.

    Here, the second class would contain the actual List; MyClass just accesses it through its own Player object.
    Last edited by Cat; 03-13-2010 at 09:34 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    That still wont work. Lets use a baseball game for example. A baseball game would be a class and it has Innings, so those innings would be a class with seperate methods to access the top or botton of the inning, with scores, etc. Well a baseball game would have more than 1 inning, so I want to use a List<Innings> variable (keeping it private), but I want that variable to have a public indexer.... so...

    BaseBallClass game = new BaseBallClass();

    game.Inning[1].TopOf();
    game.Innine[1].BottomOf(int runs);
    game.Inning[4].BottomOf();

    Code:
    public class BaseBallClass
        {
            private List<Innings> inning = new List<Innings>();
    
            public Innings Inning   // Here I want public Innings Inning[int index] but it wont work.
            {
                get { return inning[index]; } // make sure its within range.
                set { inning.add(value); } // use the indexer for validation purposes.
            }
    
        }
    
        public class Innings
        {
            private int topInningRuns;
            private int bottomInningRuns;
    
            public int TopOf
            {
                //get ; set ; here
            }
        }
    I think I am beginning to see that this can not be done unless I use public variables. Unless someone else has any ideas?
    The keyboard is the standard device used to cause computer errors!

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Windows Mixer Control in C# - CodeGuru

    There is sample code for Audio Mixers... Take a look at the implementation of the MixerDetails class
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The search term you're looking for is "indexed property". The following link has the information: Indexed Properties Tutorial (C#). In short, you can't use indexed properties (other than the one defined with 'this') in C#.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Quote Originally Posted by pianorain View Post
    The search term you're looking for is "indexed property". The following link has the information: Indexed Properties Tutorial (C#). In short, you can't use indexed properties (other than the one defined with 'this') in C#.
    Thanks, Yea, I've come to that conclusion, I have a specific implementation that I want to do, and its pretty much impossible. So I will just stick with accessor and mutator methods with my private List<>.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by stumon View Post
    That still wont work. Lets use a baseball game for example. A baseball game would be a class and it has Innings, so those innings would be a class with seperate methods to access the top or botton of the inning, with scores, etc. Well a baseball game would have more than 1 inning, so I want to use a List<Innings> variable (keeping it private), but I want that variable to have a public indexer.... so...

    BaseBallClass game = new BaseBallClass();

    game.Inning[1].TopOf();
    game.Innine[1].BottomOf(int runs);
    game.Inning[4].BottomOf();

    Code:
    public class BaseBallClass
        {
            private List<Innings> inning = new List<Innings>();
    
            public Innings Inning   // Here I want public Innings Inning[int index] but it wont work.
            {
                get { return inning[index]; } // make sure its within range.
                set { inning.add(value); } // use the indexer for validation purposes.
            }
    
        }
    
        public class Innings
        {
            private int topInningRuns;
            private int bottomInningRuns;
    
            public int TopOf
            {
                //get ; set ; here
            }
        }
    I think I am beginning to see that this can not be done unless I use public variables. Unless someone else has any ideas?
    No, here's what I'm suggesting:

    Code:
        public class BaseBallClass
        {
            private InningCollection innings = new InningCollection();
    
            public InningCollection Innings
            {
                get { return innings; } 
                private set { } // only for your class's internal use
            }
    
        }
    
        public class InningCollection // Probably want to implement IEnumerable here too
        {
    
            private List<Inning> innings = new List<Inning>();
    
            public Inning this[int i]
            {
                get { return innings[i]; }
                set { innings[i] = value; }
            }
    
            public void Add(Inning i)
            {
                innings.Add(i);
            }
    
            // Other stuff, like IEnumerable members, etc.
        }
    
    
        public class Inning
        {
            private int topInningRuns;
            private int bottomInningRuns;
    
            public int TopOf
            {
                //get ; set ; here
            }
        }
    Then you could do this:

    Code:
        BaseBallClass game = new BaseBallClass();
        game.Innings[2].TopOf = 5;
    That works because game.Innings returns an InningCollection, and then [2] references the second Inning in the InningCollection.

    By making InningCollection support IEnumerable you can even do things like:

    Code:
    foreach (Inning i in game.Innings)
    {
    }
    Essentially, the main class has a property that returns a collection class, and the collection class is indexable, making effectively an indexed named property. This is used a lot in Windows Forms, for example:
    listbox1.Items[0] -- .Items returns a collection object that can be indexed, iterated over, etc.

    Yes, at first it seems like it adds a lot of overhead, it adds an additional class to the mix, but that class consists of code you'd already need -- code to index and iterate your collection -- and it keeps all that code in one place, separated from everything else.
    Last edited by Cat; 03-15-2010 at 01:52 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Thank you Cat. I see now where I was going wrong.
    The keyboard is the standard device used to cause computer errors!

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Cat View Post
    Yes, at first it seems like it adds a lot of overhead, it adds an additional class to the mix, but that class consists of code you'd already need -- code to index and iterate your collection -- and it keeps all that code in one place, separated from everything else.
    Exactly. Indexed properties have their failings: you can't enumerate over them and you can't ask for a count to know which indices are valid. With a separate class, you can define all that and the logic stays in one place.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21