Click here to Skip to main content
16,018,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a collection of players on a basketball team which contains a hashtable within the collection to lookup players by their JerseyNumber.

However the issue I have is that I cannot simply overload my Item property that returns a player based on an index value as the overloaded method would except a integer type as well.

What I need to know is it possible to include two parameters in the default Item property:

1. For index parameter
2. For the jerseynumber

and have the property determine which argument was supplied and use the appropriate method to return a player?
Posted

Formally, this is the indexed property. This is how:
C#
class PlayerSet {
 
   //...

   internal Player this[int index, int jerseyNumber] {
      get {
         return FindPlayer(index, jerseyNumber); // but it's up to you what you return; it could be contradictory
      } //get Default
   } //Default

} //class PlayerSet


However, it's questionable if the whole idea makes sense. Usually, you can find one single item by index, or zero items, and one single item by key, or zero items. The key and index may contradict. Perhaps you should better use two different property, any this was the reason of many questions, because many people failed to figure out the syntax. One of the problems is identical types of the index and the key. This is one of the ways:
C#
class JerseyNumber { // just the wrapper, to create different type; you can think of something different
   internal JerseyNumber(int index) { this.Index = index; }
   internal int Index { get; private set; }
} //class JerseyNumber

interface IKeyedPlayerSet {
    Player this[JerseyNumber jerseyNumber] { get; }
} //interface IKeyedPlayerSet

class PlayerSet : IKeyedPlayerSet {

    public Player this[JerseyNumber jerseyNumber] {
       return FindPlayerByKey(jerseyNumber.Index); // by integer key
    } //IKeyedPlayerSet.this

    internal Player this[int index] { /* access by index */ }

    //...
   
} //class PlayerSet
These are just the ideas. I don't know some important detail of your implementation of player collection, so something in my samples may be not adequate to it, but I hope you still can figure out what to do.

—SA
 
Share this answer
 
Comments
Ron Beyer 14-Aug-13 13:20pm    
The first example would work, but would require something to show that you wanted to select by index or jersey (for example, setting one to -1 would mean you want to use the other).

However because of the complexity and odd syntax, I wouldn't do that in production. The second example is much more along the lines of what I was thinking, except that I would create a PlayerCollection which derives from Hashtable and have the two methods GetByIndex and GetByJersey in it to relieve the next person reading it from trying to figure out what it does.

+5
Sergey Alexandrovich Kryukov 14-Aug-13 13:25pm    
Thank you, Ron. Yes, I though about this -1 and something like that. To me, it does not look practical. I would not do it in any case.
—SA
No, two identically named methods (in this case Item) can't have the same prototypes. The compiler would not be able to resolve the ambiguity (which is almost what the compiler error says, ambiguous method).

For this I would remove the ambiguity entirely and create a type that derives from Hashtable. Add two methods, GetByIndex and GetByJersey. Think about the person reading the software later, how are they supposed to know that 32 is the jersey number and not the index?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Aug-13 12:49pm    
Well... there are some delicate moments here. In particular, it's possible to have two different indexed properties. A bit tricky. Please see my answer.
—SA
Ron Beyer 14-Aug-13 13:18pm    
Right, you can have multiple index/item properties, but they can't have the same prototype :) For example, you can't have:

public Player this[int index] { ... }

and

public Player this[int jerseyNumber] { ... }

Since to they compiler they both look like public Player this[int] { ... }, so its an ambiguous method. See my comments on your solution below too.
Sergey Alexandrovich Kryukov 14-Aug-13 13:23pm    
Of course, and I tried to explain it in my answer. It needs different index types...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900