473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why are there no indexed properties?

Why are there no indexed properties in C#?

For example:

class blah
{
public Node Child[index]
{
get
{
..... some code
return someNode[index];
}
set
{
... some code
someNode[index] = value;
}
}

I prefer to not return a reference to the entire data structure containing
the nodes. Also, doing so prevents me from having much control over
exactly what I am returning, unless it is always just blatantly
someNode[index].
Nov 15 '05 #1
8 1568
Russell Bearden wrote:
Why are there no indexed properties in C#?


There is but you only get one:

this[index]

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
Well, there are indexers.

public Node this[index]{
...
}

You can even overload the indexers

public Node this[index, str]{
...
}

Indexers compile into properties in the CLR.
"Russell Bearden" <ru***@mail.utexas.edu> wrote in message
news:c0**********@geraldo.cc.utexas.edu...
Why are there no indexed properties in C#?

For example:

class blah
{
public Node Child[index]
{
get
{
..... some code
return someNode[index];
}
set
{
... some code
someNode[index] = value;
}
}

I prefer to not return a reference to the entire data structure containing
the nodes. Also, doing so prevents me from having much control over
exactly what I am returning, unless it is always just blatantly
someNode[index].

Nov 15 '05 #3
> Well, there are indexers.

public Node this[index]{
...
}

You can even overload the indexers

public Node this[index, str]{
...
}

Indexers compile into properties in the CLR.

No, he was asking for "Named Indexers". Such a thing does not exist in C#
and I wonder why, since in VB.NET they are there.
It would be a great thing having them in C#, they would make things a lot
easier.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #4
I know there is a difference between properties with parameters and indexer.
I am also familiar with the VB method of doing things.

His example implied the use of indexers (although me may not have been aware
of such a construct). He wanted something like "return someNode[index]"
which is the exact syntax for indexers. Now, I do not know what you mean by
named indexers since C# does not use the name of the index property anyway
(the property by default is named Item). You can change the name that other
systems see by using IndexNameAttribute.
"cody" <do*********************@gmx.de> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
Well, there are indexers.

public Node this[index]{
...
}

You can even overload the indexers

public Node this[index, str]{
...
}

Indexers compile into properties in the CLR.

No, he was asking for "Named Indexers". Such a thing does not exist in C#
and I wonder why, since in VB.NET they are there.
It would be a great thing having them in C#, they would make things a lot
easier.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #5
The OP wanted to have functionality like this:

NodeChild nc = myNode.Child[10];

Without having to return an array. What he wanted, was a named indexer, not
the anonymous one that C# provides.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

"Peter Rilling" <pe***@nospam.rilling.net> schrieb im Newsbeitrag
news:ur**************@TK2MSFTNGP10.phx.gbl...
I know there is a difference between properties with parameters and indexer. I am also familiar with the VB method of doing things.

His example implied the use of indexers (although me may not have been aware of such a construct). He wanted something like "return someNode[index]"
which is the exact syntax for indexers. Now, I do not know what you mean by named indexers since C# does not use the name of the index property anyway
(the property by default is named Item). You can change the name that other systems see by using IndexNameAttribute.
"cody" <do*********************@gmx.de> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
Well, there are indexers.

public Node this[index]{
...
}

You can even overload the indexers

public Node this[index, str]{
...
}

Indexers compile into properties in the CLR.

No, he was asking for "Named Indexers". Such a thing does not exist in C# and I wonder why, since in VB.NET they are there.
It would be a great thing having them in C#, they would make things a lot easier.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


Nov 15 '05 #6

"cody" ...
No, he was asking for "Named Indexers". Such a thing
does not exist in C# and I wonder why, since in VB.NET
they are there. It would be a great thing having them
in C#, they would make things a lot
easier.


Though it's a bit of a hassle, you can create the similar behaviour, by just
adding another level of indirection (I believe there's a thread on the
indirection subject somewhere too).
class blah
{

Child theChild;

...

public Node Child
{
get
{
return theChild;
}
}
}
class Child
{

SomeNodeCollection children;

...

public Node this[index]
{
get
{
return children[index];
}
set
{
children[index] = value;
}
}
}

So to implement the behaviour the OP asked for wouldn't make anything
easier, but this would at least hide the reference to the actual collection,
and instead return a wrapper to the collection.

// Bjorn A
Nov 15 '05 #7
On Sun, 8 Feb 2004 00:58:21 +0100, cody <do*********************@gmx.de>
wrote:
The OP wanted to have functionality like this:

NodeChild nc = myNode.Child[10];

Without having to return an array. What he wanted, was a named indexer,
not
the anonymous one that C# provides.


I believe that is the Visual Basic way of doing it as myNode.Child(10)
(VB) would translate to myNode[10] (C#)
Same with things having a Thingy.Item(x) (VB) -> Thingy[10] (C#)

I could be very wrong.

--
The hotmail account will most likely not be read, so please respond only
to the news group.
Nov 15 '05 #8
There are only two problems with this translation from VB.
1) you aren't looking for the 10th node (which is what the code looks like
it's trying to do. can you tell by the C# indexer consumer code if you are
trying to get the 10th child vs the 10th sibling, for example?), you're
looking for the 10th Child on a node. The C# indexer can get confusing if
you aren't allowed to specify a non-default name
2) becaue the indexers aren't named in C#, you can't have more than one (you
can only override the default one, but that doesn't allow for certain
property semantics). this is silly, because behind the scenes, C# is in fact
creating a named property in IL, and applying the "default" meta-tag to it.
It wouldn't be too much work to create named properties with arguments that
don't have the "default meta-tag, as VB does.

However the above two issues are more along the lines of theory with regards
to the absense of named parameters with arguments - a better way to solve
the actual problem in this particular example as you have presented it
(which works fine in C#) is to create a readonly Child property on the node,
which is of type Nodes (a collection of Node), and have an indexer on *it*.
That allows you to use:

myNode.Child[10]
instead of
myNode[10]

-Rob Teixeira [MVP]

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...
On Sun, 8 Feb 2004 00:58:21 +0100, cody <do*********************@gmx.de>
wrote:
The OP wanted to have functionality like this:

NodeChild nc = myNode.Child[10];

Without having to return an array. What he wanted, was a named indexer,
not
the anonymous one that C# provides.


I believe that is the Visual Basic way of doing it as myNode.Child(10)
(VB) would translate to myNode[10] (C#)
Same with things having a Thingy.Item(x) (VB) -> Thingy[10] (C#)

I could be very wrong.

--
The hotmail account will most likely not be read, so please respond only
to the news group.

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kor | last post by:
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...
2
by: Daniel Miller | last post by:
What is the difference between indexers and indexed properties (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkindexedpropertiestutorial.asp)? I don't see what...
3
by: Fin | last post by:
Index properties in C++ class libraries (.NET) apper as set_ and get_ methods when used in C# To test this out, I changed the example from section "13.2 Indexed Properties" in MSDN, and placed the...
1
by: Mike | last post by:
Hi, I have created a collection of a custom class. Everything works fine (can add these items in a combo from within a VB.NET application, for instance), but when looking at the collection from a...
1
by: Gokul | last post by:
Hi, I'm using indexed properties in an object which acts as the binding source. How can I implement INotifyPropertyChanged for that object so that when the indexed property is updated, binding...
1
by: solar | last post by:
Indexed.Yes (Duplicates OK). In my code for remote control i wanted to remove the property Indexed of a field, but i receive the date type conversion error.How can i change these properties by...
7
by: Tom Dacon | last post by:
I'm using Reflection to iterate over the properties and fields of an arbitrary object. For non-indexed properties it's pi.GetValue(theObject, Nothing) for VB, or pi.GetValue(theObject, null) for...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: saudamini | last post by:
i am trying to deserlize XML against the class which has this an indexed property, the deserializer just skips this indexed property, it's not setting it. I remember reading somewhere XML...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.