indexer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi

I have created a class with a simple indexer
what is the syntax for adding the function "Remove"
...
myClass.colors["a"]=Color.Black; // WORKS O
myClass.colors["black"].Remove(); // ??
....
ColorCollection _colors=new ColorCollection()
public ColorCollection Color

ge
{
return _colors;


....
public class ColorCollectio

public Color this[string colorName

ge
{
..

se
{
..





thanx ;-)
 
Even when a class has an indexer, it still should support the Add and Remove
methods.

You should still have a Remove method -- myClass.colors.Remove("black");
 
hi Peter

I know I can use functions with parameters - myClass.colors.Remove("black");, but I would like to use the indexer: myClass.colors["black"].Remove();

Cheers

----- Peter Rilling wrote: ----

Even when a class has an indexer, it still should support the Add and Remov
methods

You should still have a Remove method -- myClass.colors.Remove("black")

Oren said:
hi
I have created a class with a simple indexer
what is the syntax for adding the function "Remove"
..
myClass.colors["a"]=Color.Black; // WORKS O
myClass.colors["black"].Remove(); // ??
...
ColorCollection _colors=new ColorCollection()
public ColorCollection Color

ge

return _colors


...
public class ColorCollectio

public Color this[string colorName

ge

..

se

..

 
You cannot do that because the Remove method would be applied to the value
returned by the indexer. If you want to affect myClass.colors, your methods
and properties need to be called directly on the object. Since a Color
enumeration value does not support the Remove method (no enum values do),
this would not be correct syntax.

Oren said:
hi Peter,

I know I can use functions with parameters -
myClass.colors.Remove("black");, but I would like to use the indexer:
myClass.colors["black"].Remove();
Cheers.

----- Peter Rilling wrote: -----

Even when a class has an indexer, it still should support the Add and Remove
methods.

You should still have a Remove method -- myClass.colors.Remove("black");
Oren said:
hi,
I have created a class with a simple indexer.
what is the syntax for adding the function "Remove" ?
...
myClass.colors["a"]=Color.Black; // WORKS OK
myClass.colors["black"].Remove(); // ???
....
ColorCollection _colors=new ColorCollection();
public ColorCollection Colors
{
get
{
return _colors;
}
}
....
public class ColorCollection
{
public Color this[string colorName]
{
get
{
...
}
set
{
...
}
}
}
thanx ;-)
 
Hi Peter

thank you for replying so fast

what if the indexer will not return the 'Color' class ? maybe some other class with the Remove method. what will be the syntax then

thanx.
 
Hi Peter

thank you for replying so fast

what if the indexer will not return the 'Color' class ? maybe some other class with the Remove method. what will be the syntax then

thanx.
 
Hi,
Hi Peter,

thank you for replying so fast.

what if the indexer will not return the 'Color' class ?
maybe some other class with the Remove method.
what will be the syntax then ?

thanx.

If you want to create collection with Add, Remove, Clear...
Look in .NET docs for any IList implementation... e.g. ArrayList

First of all: Color, if you mean System.Drawing.Color is a
structure, not class! So, if You want to create Color indexer
the you have to think again what to return if "name" of color
does not exists in your collection.

I can recomend you to return "Color.Empty"... but i think that
new methods will be more useful too:

bool Contains(string colorName);
it returns true if colorName exists in collection.

int IndexOf(string colorName);
it returns index of color in collection or e.g. -1 if it's
not in collection

That method should help you with "set" part of indexer.
If color exists then you get collection index and set
collection element to new color value

Regards

Marcin
 
Hi Oren,

You can't do that since the Color type returned by the indexer doesn't have
Remove method (and it doesn't make sense to have)
The solution to your problem is not to have an indexer, but have a property
that returns a type that has an indexer and Remove method (collection of
colors).

Then in the class you'll have

public ColorCollection Colors
{
get{return colorCollection;}
}

So now you can make

foo.Colors["black"] = Color.Black;

or

foo.Colors.Remove("black");

the other sintax IMHO is not possible unless you don't use some wrapper
classes with overloaded type-casting operation, which is not a good idea
especialy in the case of multilanguage platforms like .NET where not all the
languages supports overloaded operators.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Oren said:
hi Peter,

I know I can use functions with parameters -
myClass.colors.Remove("black");, but I would like to use the indexer:
myClass.colors["black"].Remove();
Cheers.

----- Peter Rilling wrote: -----

Even when a class has an indexer, it still should support the Add and Remove
methods.

You should still have a Remove method -- myClass.colors.Remove("black");
Oren said:
hi,
I have created a class with a simple indexer.
what is the syntax for adding the function "Remove" ?
...
myClass.colors["a"]=Color.Black; // WORKS OK
myClass.colors["black"].Remove(); // ???
....
ColorCollection _colors=new ColorCollection();
public ColorCollection Colors
{
get
{
return _colors;
}
}
....
public class ColorCollection
{
public Color this[string colorName]
{
get
{
...
}
set
{
...
}
}
}
thanx ;-)
 
Back
Top