B
B0nj
I've got a class in which I want to implement a property
that operates like an indexer, for the various colors
associated with the class.
For instance, I want to be able to do 'set' operations like
MyClass.MyColors["Background"] = Color.Green
or, a 'get', such as
Color forecolor = MyClass.MyColors["Foreground"];
I want to use an indexer so I can take parameters, such
as the color type (e.g. "Foreground", "Background" etc.).
With a single member function I couldn't do this as I would have to
have GetColor and SetColor which seems a little bit kludgy, due
to the need to have a parameter.
However I don't want to use the indexer of the class itself
as 'MyClass["Background"] = Color.Green' hasn't really got
anything pertaining to the specific color property. Besides,
I may want to have more properties like this but color.
(I am actually using an enum rather than strings such as
"Background"/"Foreground", but you get the idea.)
So what I've done is implemented a separate 'nested'
class to provide the color property, and used its indexer
to get and set the various colors, which are then stored in an
array in the nested class.
I initially tried to make the nested class itself private, but the
color-setting property that is of the type of the nested class
public, but I got the error 'MyColors is less accessible than the
member that is of its type' error. I then of course had to make
the nested class public, presumably because the information
about its type and thus its interface has to be made available
to the client, but it's not the sort of class that the client should
be able to create an instance of, so I've declared its constructor
internal, so it can't be instantiated outside of the project.
But I am wondering if this is the best way to do it, or the right
use for nested classes - as the client can still see the nested class,
just can't instantiate it. I'm reasonably happy enough with this
prevention because it would generate a compile-time error, not
a runtime one, but I was wondering if there was a better way?
The aim really is to make the syntax for the color-setting functionality
as simple and intuitive to the client as possible.
Any ideas?
that operates like an indexer, for the various colors
associated with the class.
For instance, I want to be able to do 'set' operations like
MyClass.MyColors["Background"] = Color.Green
or, a 'get', such as
Color forecolor = MyClass.MyColors["Foreground"];
I want to use an indexer so I can take parameters, such
as the color type (e.g. "Foreground", "Background" etc.).
With a single member function I couldn't do this as I would have to
have GetColor and SetColor which seems a little bit kludgy, due
to the need to have a parameter.
However I don't want to use the indexer of the class itself
as 'MyClass["Background"] = Color.Green' hasn't really got
anything pertaining to the specific color property. Besides,
I may want to have more properties like this but color.
(I am actually using an enum rather than strings such as
"Background"/"Foreground", but you get the idea.)
So what I've done is implemented a separate 'nested'
class to provide the color property, and used its indexer
to get and set the various colors, which are then stored in an
array in the nested class.
I initially tried to make the nested class itself private, but the
color-setting property that is of the type of the nested class
public, but I got the error 'MyColors is less accessible than the
member that is of its type' error. I then of course had to make
the nested class public, presumably because the information
about its type and thus its interface has to be made available
to the client, but it's not the sort of class that the client should
be able to create an instance of, so I've declared its constructor
internal, so it can't be instantiated outside of the project.
But I am wondering if this is the best way to do it, or the right
use for nested classes - as the client can still see the nested class,
just can't instantiate it. I'm reasonably happy enough with this
prevention because it would generate a compile-time error, not
a runtime one, but I was wondering if there was a better way?
The aim really is to make the syntax for the color-setting functionality
as simple and intuitive to the client as possible.
Any ideas?