Click to See Complete Forum and Search --> : Covariant


VizOne
March 25th, 2003, 05:09 AM
Hi!

I know a similar problem has been talked over at this board, however it was always related to VC++6.


I'd like to implement my own Collection that derives from Collection Base. I want the Item-property to have the proper return type instead of Object*. In C# it is easy to implement a Covariant return type:


class MyData
{
public int Value
{
get { return 5; }
}
};

class MyCollection : CollectionBase
{
public MyData this[int index] // override indexer from IList
{
get{ return (MyData)List[index]; }
set{ List[index] = value; }
}
};


In VC++.net, however it does not seem to be possible, as covariant return types are not allowed - at least in VC++.net:


__gc class MyData
{
__property int get_Value()
{
return 5;
}
};

[Reflection::DefaultMember(S"Item")] // let Item be the indexer
__gc class MyCollection
: public System::Collections::CollectionBase
{
__property MyData * get_Item(int in_index) // C2392- Error!
{
return static_cast<MyData*>(List->Item[in_index]);
}
};



Is there any attribute or another way to achieve what I'd like to do?
I am using VS.net 2002. Might this feature be available in vs.net 2003?

Thanks in advance!

- Andre