Default accessor in MC++

  • Thread starter Daniel =?iso-8859-1?Q?Lidstr=F6m?=
  • Start date
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

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);
}
};
 
J

Jesse McGrew

Daniel said:
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;
}

Jesse
 
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Daniel said:
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;
 
J

Jesse McGrew

Daniel said:
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?

Yes.

The compiler thinks you're trying to override ArrayList's get_Item
method (which is virtual) instead of replacing it, since yours has the
same parameters, but the return type differs so you get an error. In C#,
you could use the 'new' keyword to hide the inherited member instead of
overriding it, but I don't think MC++ has an equivalent.
Is this how I use the default accessor? It compiles anyhow.

UnitsCollection* units = new UnitsCollection;
Units* unit = new Units();
units->Index[0] = unit;

Yes.

In MC++ the default accessor is no different from any other indexed
property. The [DefaultMember] attribute only affects other languages
such as C#.

Jesse
 

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