Sign in to follow this  

[.net] C# - How do I create an indexed property?

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to be able to create an indexed property on a class. Here's how I'd like to use it:
Sprite.Animations["walk_east"].AddFrame(...)

Where Animations is the property, and "walk_east" is the indexer. I'd rather not have to create a custom collection class and expose it, and I'd also like to avoid using a function that returns an Animation accepting a string if I can use a property for it instead. Here's the code I have. It doesn't compile. I'm following instructions from The C# Station, but it seems like they only apply if the object being indexed is this.
        public Animation Animations[string[] Name]
        {
            get
            {
            }
            set
            {
            }
        }



What am I doing wrong?

Share this post


Link to post
Share on other sites
That's because they are showing you how to overload the index operator. A property returns the type you assign it, so that type must have the index operator implemented for itself.

So, if you have an array of animations and you want to address them by name, use a dictionary with the string name of the animation as the key.

Share this post


Link to post
Share on other sites
Unlike Managed C++ or C++/CLI, C# does not support other indexed properties than "this". What you normally do is to provide a property that returns an object with an indexer (this-property), for example a dictionary as Washu said or some other class more tailored to your needs, e.g.



class ReadOnlyColors {
Color[] values;

public ReadOnlyColors (Color[] values) {
this.values = values;
}

public Colorthis[int index]{
get { return values[index]; }
}
}


class Data {
ReadOnlyColors backgroundColors;
ReadOnlyColors foregroundColors;


public Data() {
Color[] bg = { Color.Black, Color.Red, Color.Green };
Color[] fg = { Color.White, Color.Yellow, Color.Blue};
backgroundColors = new ReadOnlyColors(bg);
foregroundColors = new ReadOnlyColors(fg);
}


public ReadOnlyColors BackgroundColors{
get{ return backgroundColors; }
}

public ReadOnlyColor ForegroundColors {
get { return foregroundColors; }
}
}

// usage:

Data data = new Data();

Color bg1 = data.BackgroundColors[0];
Color fg1 = data.ForegroundColors[0];



This way BackgroundColors and ForegroundColors can be used like "real" indexed properties.


Regards,
Andre

Share this post


Link to post
Share on other sites

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this