changing parameter types on inherited member

G

Guest

Could someone tell me how to Change the parameter type for an override of an
inherited class member? In particular, I would like to change an indexer for
my class that implements the IList class. Such as this:

public abstract class myclass : IList
{
private ArrayList ary;

....[Cut for brevity]
public string this[int index]
{
get
{
return (string)this.ary[index];
}
}
....[Cut for brevity]
}

When I try to compile, I get (my class) "does not implement interface member
'System.Collections.IList.this[int]'." etc..

It appears I am being required to use a type of object instead of string.
I would prefer not to have to cast to string upon every call to this indexer
from other classes.

Any help would be appreciated.
 
G

Guest

Hi Walt,

implementing IList requires you to provide a method with the exact signature
as declared in the interface, in this case "Object this[Int32] {get;set;}".
If you change the returntype, you're no longer implementing the interface.

There's a simple way to do want you want: leave your indexer, but add the
following (untested):
Object IList.this[Int32 index]
{
get
{
return this[index];
}
set
{
...
}
}

You'll find more info by googling on "explicit interface implementation";

hth, Baileys
 
G

Guest

Your suggestion of adding the explicit inherited member without the public
keyword, and including the base class name in the method name resolved my
issue. Thank you very much!

Baileys said:
Hi Walt,

implementing IList requires you to provide a method with the exact signature
as declared in the interface, in this case "Object this[Int32] {get;set;}".
If you change the returntype, you're no longer implementing the interface.

There's a simple way to do want you want: leave your indexer, but add the
following (untested):
Object IList.this[Int32 index]
{
get
{
return this[index];
}
set
{
...
}
}

You'll find more info by googling on "explicit interface implementation";

hth, Baileys

Walt Zydhek said:
Could someone tell me how to Change the parameter type for an override of an
inherited class member? In particular, I would like to change an indexer for
my class that implements the IList class. Such as this:

public abstract class myclass : IList
{
private ArrayList ary;

...[Cut for brevity]
public string this[int index]
{
get
{
return (string)this.ary[index];
}
}
...[Cut for brevity]
}

When I try to compile, I get (my class) "does not implement interface member
'System.Collections.IList.this[int]'." etc..

It appears I am being required to use a type of object instead of string.
I would prefer not to have to cast to string upon every call to this indexer
from other classes.

Any help would be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top