Indexed properties in .NET

  • Thread starter Thread starter Kor
  • Start date Start date
K

Kor

Hi,

Am I right that indexed properties are implemented in VB
..NET, but NOT in C#. It seems that for C# you have to wrap
the property in a separate class, for emulating index
properties.

In addition in VB .NET you can use indexed property, but
with only an integer as indexer. So, a string as indexer (as
in VB6) will not work here. Is that right ?

Some help would be nice.

Thanks.

K.
 
Am I right that indexed properties are implemented in VB
.NET, but NOT in C#. It seems that for C# you have to wrap
the property in a separate class, for emulating index
properties.
You have indexers in on classes in C#.

The following snippet shows how to implement an numeric index on a class:
class ClassWithIndexer {
private string[] data = new string[5];
public string this [int index] {
get {
return data[index];
}
set {
data[index] = value;
}
}
}
In addition in VB .NET you can use indexed property, but
with only an integer as indexer. So, a string as indexer (as
in VB6) will not work here. Is that right ?
The following snippet shows how to hide a hashtable behind an indexed
property in VB.NET.

Public Class ClassWithIndexedProperty
Private m_data As New Hashtable
Public Property Data(ByVal key As String) As String
Get
Return CStr(m_data(key))
End Get
Set
m_data(key)=Value
End Set
End Property
End Class

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Anders Norås said:
You have indexers in on classes in C#.

In other words Yes you are right, C# does NOT support indexed properties.
You have to wrap your property/array in a new class and use an Indexer in the
class.

I'm in the same boat, I wanted to have several indexed properties in my
class, but decided to just expose the arrays publicly rather then spend an
extra week creating all the classes needed. Rather big flaw in the design if
you ask me, (but then it's a new product really so I'm suprised it actually
works)
 
Back
Top