471,605 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

changing parameter types on inherited member

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.

Jan 8 '06 #1
2 2063
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" wrote:
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.

Jan 8 '06 #2
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" wrote:
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" wrote:
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.

Jan 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dave | last post: by
3 posts views Thread by Wayne Brantley | last post: by
13 posts views Thread by dragoncoder | last post: by
3 posts views Thread by kikazaru | last post: by
1 post views Thread by interX | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.