Howto write a class where a property can be accessed without naming it.

Posted by SchlaWiener on Stack Overflow See other posts from Stack Overflow or by SchlaWiener
Published on 2010-03-29T07:05:34Z Indexed on 2010/03/29 7:13 UTC
Read the original article Hit count: 269

Filed under:
|
|
|

I have a (dump) question regarding VB/C#

I often use third party classes where I can access a child object with only specifying the id or key.

Example:

Instead of writing:

DataRow row = GetAPopulatedDataRowSomeWhere();
Object result = row.Items[1]; // DataRow has no Items property
Object result = row.Items["colName"]; // Also not possible

I use this code to access the members:

DataRow row = GetAPopulatedDataRowSomeWhere();
Object result = row[1];
Object result = row["colName"];

Can someone tell me how a class has to look like to support this syntax? My own class has a Dictionary that I want to access this way.

MyClass["key"]; // <- that's what I want
MyClass.SubItems["key"]; // <- that's how I use it now

© Stack Overflow or respective owner

Related posts about c#

Related posts about vb.net