Click to See Complete Forum and Search --> : C++ and default properties


darwen
April 23rd, 2004, 10:56 PM
I notice that classes such as System::Collections::ArrayList have default properties such as ::Item which can be used in the following way in C# -



ArrayList asArray = new ArrayList();
asArray.Add("Hello");

String sString = asArray[0];



When implementing a .NET assembly in C++ is there any way of defining default properties in this way. The only way I've found which is picked up by C# is to use get_ and set_ functions.

So, I want to be able to (for example)


// in C++ assembly
class StringArray : public ArrayList;

// in C#, what I want
StringArray asStrings = new StringArray();

String sString = asStrings[0];

// the only way I've found of doing it :
String sString = asStrings.get_String(0);


Many thanks,

Darwen.

TheCPUWizard
April 23rd, 2004, 10:58 PM
1) There is only one default property (not plural), although it can be polymorphic.

2) The trick is to call it Item.

darwen
April 24th, 2004, 02:40 AM
Yes, I know I can call item and cast

e.g.


// C#
StringArray asStrings = new StringArray();
asStrings.Add("Hello");

String sString = (String)asStrings.Item[0];


However I want to be able to do this without the cast. In the above case the Item property is the default property.

However, since the return type of Item is going to be different (in my case a String instead of an Object) it won't let me override it.

Darwen.

Andy Tacker
April 26th, 2004, 10:50 AM
String sString = asStrings.Item[0].ToString();