473,795 Members | 3,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1585
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.ute xas.edu> wrote in message
news:c0******** **@geraldo.cc.u texas.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 IndexNameAttrib ute.
"cody" <do************ *********@gmx.d e> wrote in message
news:uB******** ******@TK2MSFTN GP10.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.r illing.net> schrieb im Newsbeitrag
news:ur******** ******@TK2MSFTN GP10.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 IndexNameAttrib ute.
"cody" <do************ *********@gmx.d e> wrote in message
news:uB******** ******@TK2MSFTN GP10.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
{

SomeNodeCollect ion 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.d e>
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.m icrosoft.com...
On Sun, 8 Feb 2004 00:58:21 +0100, cody <do************ *********@gmx.d e>
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
5688
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 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 ?
2
2178
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 the problem is with using plain indexers is. Let's just say I'm confused. :P
3
2759
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 Employee and Manager classes in a library. Then I wrote a C# application implement the code from main(). What happens is that everyting works fine except when trying to call the indexed properties Example The following line does not work (as it...
1
3388
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 VB.NET application in the Watch window, I cannot see the items of the collection. The message in the Watch window's "value" says: "<cannot view indexed property>." Below is the code I wrote. Any idea of what is wrong? Or is the error message...
1
3365
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 target is updated? To be more specific please find the code below.. public class ClassA : INotifyPropertyChanged
1
6194
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 code ? At the end of the list of the properties of the field,just below Required, it is written Indexed.Yes (Duplicates OK). i want to turn it to No and i write False, but it says property is unkown. What is the exact property and the command to...
7
16572
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 C# For indexed properties, instead of Nothing (null) you pass an array of index values. OK, no problem so far.
17
2787
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 Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain property of an object is just an ordinary attribute. But that applies to
0
1144
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 serialization does not convert methods, indexers, private fields, or read-only properties (except read-only collections). am i correct? how can i serialize/deserialize an indexed property? Any idea why?
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.