By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

Indexed properties in .NET

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/
Jul 21 '05 #2

P: n/a
"Anders Norås [MCAD]" wrote:
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#.


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)
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.