On Thu, 27 May 2004 21:48:00 -0700, Jesse McGrew wrote:
Daniel Lidström wrote: I have a CS class I wish to write in MC++:
public class UnitsCollection : ArrayList
{
new public Units this[int index]
{
get { return (Units) base[index]; }
set { base[index] = value; }
}
}
Would the MC++ class below be equivalent?
[DefaultMember("Item")]
public __gc class UnitsCollection : public ArrayList
{
public:
__property Units* get_Item(int index) {
return (Units*)__super[index];
}
__property void set_Item(Units* u) {
__super::Add(u);
}
};
MC++ doesn't support C#'s indexer syntax.. instead of
'collection[index]' you must write 'collection->Item[index]', and so on.
In this case you need '__super::Item[index]'. Also, since get_Item takes
an index, set_Item should also.
Try changing the property accessors as follows:
__property Units* get_Item(int index) {
return (Units*)__super::Item[index];
}
__property void set_Item(int index, Units* u) {
__super::Item[index] = u;
}
When I try this I get the following compile error:
error C2392: 'LX::Units __gc *LX::UnitsCollection::get_Item(int)' :
covariant returns types are not supported in managed types
I think it means Item is already in the baseclass. Would it be a solution
to change to
__property Units* UnitsCollection::get_Index(int index) {
return (Units*)__super::Item[index];
}
__property void UnitsCollection::set_Index(int index, Units* u) {
__super::Item[index] = u;
}
And put [System::Reflection::DefaultMemberAttribute("Index" )] attribute on
the UnitsCollection class?
Is this how I use the default accessor? It compiles anyhow.
UnitsCollection* units = new UnitsCollection;
Units* unit = new Units();
units->Index[0] = unit;
--
Daniel