[Mono-devel-list] overriding return type

Jacob Eggleston jacobmono at yahoo.com
Sat Jul 26 21:37:38 EDT 2003


After reading through your email again, I think I
understand a little better what you're trying to do. 

See if this is along the lines of what you're
thinking. Note that I changed the two Iterator
interfaces to classes for the sake of making the code
a little shorter. 

interface IList {
	IIterator Iterator();		
}

class IIterator {
	bool Next(){return true;}
	object Item {
		get{return this;}
	}		
}

interface IList2 : IList {
	// use "new" to make a member with
	// the same name, but a different
	// return type.
	new IIterator2 Iterator();
}

class IIterator2 : IIterator {
	bool Prev(){return true;}
}

// In the class that actually implements
// the interfaces, we can return the 
// correct type, depending on which
// interface is being called.
class MyList: IList2, IList{
	public IIterator2 Iterator() {
		return new IIterator2();
	}
	IIterator IList.Iterator() {
		return (IIterator)this.Iterator();
	}
	IIterator2 IList2.Iterator() {
		return this.Iterator();
	}
}


Using the above code in the following way works like I
believe you want it to. (And it even compiles :)

IList2 list = new MyList();
list.Iterator(); // This returns an IIterator2
((IList)list).Iterator(); // This returns an IIterator

Regards,
Jacob Eggleston


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com



More information about the Mono-devel-list mailing list