-3

In C# is it possible to have an Indexer property of a type that is not a string or an int?

For example I have a custom object that is a map of 2D vector coordinates. Taking the basics of my map class to be...

public class TileMap
{
    /// <summary>
    /// The holds an array of tiles
    /// </summary>
    MapTile[,] _map;

    /// <summary>
    /// Gets the <see cref="PathFindingMapTile"/> with the specified position.
    /// </summary>
    /// <value>
    /// The <see cref="PathFindingMapTile"/>.
    /// </value>
    /// <param name="position">The position.</param>
    /// <returns></returns>
    public MapTile this[Vector2 position]   // Indexer declaration
    {
        get
        { 
            int x = (int)position.x;
            int y = (int)position.y;
            return _map[x, y]; 
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="TileMap"/> class.
    /// </summary>
    /// <param name="length">The length.</param>
    /// <param name="height">The height.</param>
    public TileMap(int length, int height)
    {
        _map = new MapTile[length, height];
    }

}

This compiles without issue. However the calling code fails with two errors

Base class

internal abstract class MyBase
{
    /// <summary>
    /// Gets (or privately sets) the tile map
    /// </summary>
    protected PathFindingMapTile[,] TileMap { get; private set; }
}

derived class

internal class MyDerived : MyBase
{
    public void MyMethod()
    {
        Vector2 possiblePosition;
        MapTile possibleTile = null;

        possibleTile = this.TileMap[possiblePosition]; // <-- This line wont compile
    }
}

Compile Errors:

Cannot implicitly convert type 'UnityEngine.Vector2' to 'int' 
Wrong number of indices inside []; expected 2

Why two indices? I have stated just one, the position. Any suggestions?

Update - Following Rufus comment. Corrected return type of "Tilemap" property of the base class.

internal abstract class MyBase
{
    /// <summary>
    /// Gets (or privately sets) the tile map
    /// </summary>
    protected TileMap TileMap { get; private set; }
}
Dib
  • 2,001
  • 2
  • 29
  • 45
  • You are treating the indexers as it would be a static property, but that is not the case. You needt instance of TileMap in order to use indexer? – Erti-Chris Eelmaa Feb 26 '15 at 22:25
  • 2
    Please show more of the calling code - in particular, do you have a *variable* called `TileMap`? (Just for clarity, try naming it something other than the name of your type...) – Jon Skeet Feb 26 '15 at 22:25
  • Well... I have got the calling code to compile using the following. possibleTile = TileMap[(int)possiblePosition.x, (int)possiblePosition.y]; ...but that is clutching at straws! – Dib Feb 26 '15 at 22:25
  • 1
    Well it sounds like `TileMap` isn't of the type `TileMap` - but you need to show us more code... – Jon Skeet Feb 26 '15 at 22:26
  • 1
    Sorry I have not been clear. TileMap refers to a property of the same name as the type. I'll update the code! – Dib Feb 26 '15 at 22:29
  • 1
    Your `TileMap` property is not of type `TileMap`, it's of type `PathFindingMapTile[,] `, which requires two indexes. – Rufus L Feb 26 '15 at 22:37

1 Answers1

1

The problem is that your TileMap property is not of type TileMap, it's of type PathFindingMapTile[,], which requires two indexes.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Sorry, I have now cleared up the example to show that "TileMap" is not referring to the static instance but is an instance property which has the same name as the type. – Dib Feb 26 '15 at 22:37
  • My bad. TileMap was originally an array of PathFindingMapTile, but I refactored that to be a TileMap object which had the array as an internal member. I had forgotten to refactor the property return type. Thanks again. – Dib Feb 26 '15 at 22:43
  • Much too late in the evening and way too tired to spot it for myself! – Dib Feb 26 '15 at 22:51