1
public string name;
public int rollNo;

public string this[int i] 
{
    get 
    {
        switch(i)
        {
            case 0:
                return name;

            case 1:
                return rollNo.ToString();

            default:
                return "";
        }
    }
    set
    {
        switch (i)
        {
            case 0:
                name = value;
                break;
            case 1:
                rollNo = value;
                break;
        }
    }
}

above is the code that I'm trying to execute but the problem is rollNo = value shows this problem "Cannot convert from string to int". What I'd like to ask is, Does the value passed to the indexer need to be the same as the return type of indexer? If no please guide me what exactly I'm doing wrong. -Thanks

david.s
  • 11,283
  • 6
  • 50
  • 82
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101
  • 1
    indexer has to be integer type, not the return type – Kamil Budziewski Jul 08 '13 at 06:07
  • Sorry! can't get your point. Would you please be more clear? – Kamran Ahmed Jul 08 '13 at 06:09
  • @wudzik: not necessarily.. See this -> [Real world use cases for C# indexers?](http://stackoverflow.com/questions/2185071/real-world-use-cases-for-c-sharp-indexers) – Paritosh Jul 08 '13 at 06:10
  • `value` is of the same Type that is specified by the indexer. In this case, it is a string. `i` is an `int`, as specified in your code. – brianestey Jul 08 '13 at 06:10
  • Seems pretty clear to me - for some reason you've declared your indexer to be of type `string`, so the type of `value` is string. Your design is very odd though - I wouldn't use an indexer for this. – Jon Skeet Jul 08 '13 at 06:10
  • @JonSkeet I was just testing my knowlege of indexers using this, it was never meant to be used for some real world application. – Kamran Ahmed Jul 08 '13 at 06:14

2 Answers2

8

Does the value passed to the indexer need to be the same as the return type of indexer?

Yes, it is by definition... just like the type of value in a property setter is also the same type as the property. You can't have an indexer or property which has one type for setting and a different type for getting. In your case you wouldn't want that anyway, as then you wouldn't be able to set name...

It's unclear why you're trying to use an indexer for this at all. It would be far clearer to just use two properties:

public string Name { get; set; }
public int RollNumber { get; set; }

That way no conversions are required at all. It's worth avoiding conversions unless you really need them.

(Additionally, your fields are already public, which is also a bad idea... fields are an implementation detail which should generally be hidden. Note that the automatically-implemented properties above mean you wouldn't even need to explicitly declare fields.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

value is going to be the type of the property, in your case string.

The simple solution would be to do:

rollNo = int.Parse(value);
rossipedia
  • 56,800
  • 10
  • 90
  • 93