C#: using get and set methods with arrays

Started by
4 comments, last by hereticprophecy 16 years, 10 months ago
I have an array of Colors that I use to highlight selections in a menu, and I would like to use get{} and set{} methods to automate highlighting (menuColor[2] gets highlighted, so all of the others return to the base color). If I run this code to alter a field in my array... menuColor[2] = new Color(255,0,0,255); ...how can I dynamically access that "2" in set{}? From there, it's easy to alter the others with the base color, but I don't know if this is even possible. It seems like a pain to use IEnumerable to find which index is being used, but that's my current solution. Any other ideas?
Advertisement
I don't have C# on my computer right now.
You can use "indexer".
but I think it's something like this.

public Color this[int index]{  set  {     menuColor[index] = value;  }  get  {     return menuColor[index];  }}


Thanks for the reply.

After stumbling across this page, it seems as though value does the indexing all on it's own (how thoughtful). So, if that is indeed the case, it would make a lot more sense to simply return all fields to the base color, then let value do it's work, like so...

Color [] Col_Top{	get	{		return col_Top;	}	set	{		for(int i = 0; i < ( TopStatus.GetNames() ).GetLength(); i++)		{			col_Top = new Color(0,0,255,255);		}		col_Top = value;	}}


I'm not sure how value indexes, so the fact that I run through the whole array may mess that up.
I think you are a little confused as to how this works. When you return an array using a getter, you are returning the whole array. Likewise, when setting an array, you are setting the whole array. You cannot set or get individual elements from your class. The array class that you are returning handles all of the that. So the following code:
YourClass.Col_Top[0] = new Color();

Is ONLY using the getter. The setter never gets used. The setter is only used when the following happens:
YourClass.Col_Top = new Color[4];

If you are indexing into the array, you are using the getter.

This leads to a few comments about the way you are trying to do this. First, its usually a bad idea to allow a setter for an array, as that allows the outside users to completely replace the base array. Second, you should probably be using a function instead, or just make your own collection class to handle the collection of colors. That way you can provide a this[int index] getter and setter on your own custom collection and add your special handling there.

The easiest way of course would to just move the two issues into methods, such as
Color GetTopColor(int index)
and
void SetTopColor(int index, Color color)
.

If you have any other questions, be sure to ask!
Mike Popoloski | Journal | SlimDX
Hold on, why should the "outside" code either be allowed to, or have to choose the colour for a highlighted item? Instead, have a property to indicate which item is highlighted, and do the colour-changing logic in its setter:

int highlighted_;int highlighted {  get { return highlighted_; }  set { menuColor[highlighted_] = NON_HIGHLIGHTED_COLOR;        highlighted_ = value;        menuColor[highlighted_] = HIGHLIGHTED_COLOR;      }}


Although, you probably *really* want to create a structure to represent menu items as well, instead of having two separate arrays of strings and colour-values. (It's especially ugly to be checking the length of the string array when deciding how many colours to set!)

Although as well, you probably don't even want this level of control from the outside. Consider just having separate scrollUp()/scrollDown() methods instead of a setter on a property... outside code shouldn't have to check the value, do math and write back just to scroll the menu, when the menu is already responsible for remembering what's highlighted.
The outside code really doesn't have any access to this property...in fact the only public methods are the constructor and the Update() method, which does all of the handling for highlighting and user input.

The whole point in this class is to make it simple for someone to add a new option to this menu: as it stands, you only need to add the name to the enumeration and an additional case in the update switch. If one of our designers wants to add a "Screen Positions" function, they don't have to bother us to do it: they just need to add it to the enum and pseudo-code the case with a TODO.

This topic is closed to new replies.

Advertisement