471,337 Members | 977 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Indexers and Item()

I am writing a collection. I downloaded the Rotor kit and started looking at
how the classes in System.Xml are written and am using them as a guide. For
my collection, I thought I'd use XmlNodeList as my pattern.

Two particular things stand out:

public abstract XmlNode Item(int i);

and

public virtual XmlNode this [int index] {}

I created an indexer in my collection class without a problem. When I added
an Item() method to it, however, I was told that Item was already in use.
The debugger pointed to the indexer as the culprit. The compiler will not
continue.

So my question is this: How can I use an indexer and an Item() method in the
same class?

Thanks!
Jan 12 '06 #1
5 1490
I guess I should also note that commenting out either the Item() method or
the indexer results in a successful compile.
"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:u2*************@tk2msftngp13.phx.gbl...
I am writing a collection. I downloaded the Rotor kit and started looking
at how the classes in System.Xml are written and am using them as a guide.
For my collection, I thought I'd use XmlNodeList as my pattern.

Two particular things stand out:

public abstract XmlNode Item(int i);

and

public virtual XmlNode this [int index] {}

I created an indexer in my collection class without a problem. When I
added an Item() method to it, however, I was told that Item was already in
use. The debugger pointed to the indexer as the culprit. The compiler will
not continue.

So my question is this: How can I use an indexer and an Item() method in
the same class?

Thanks!

Jan 12 '06 #2
Jeremy,

By default, the indexer of a class will be represented as an Item method
with the same parameter list and return type. If you look at the IL for an
indexer (represented here with an int parameter and an int return type), you
will see that it is represented as a property with the name "Item":

.property int32 Item
{
.get instance int32
ConsoleApplication79.Program::get_Item(int32)
}
Also, you will notice there is an attribute on the class level:

.custom instance void
[mscorlib]System.Reflection.DefaultMemberAttribute::.ctor(st ring) = (
string("Item") )

This indicates that the Item property is the default member (indexer).

Now, if you want to have an Item method, and an indexer, then you have
to attribute the indexer with the IndexerName attribute (located in the
System.Runtime.CompilerServices namespace), indicating what name you want
your indexer to have.

However, one has to ask why you would do this, since it seems you are
doing the same thing as the indexer, so why not just let the indexer do what
it does?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:u2*************@tk2msftngp13.phx.gbl...
I am writing a collection. I downloaded the Rotor kit and started looking
at how the classes in System.Xml are written and am using them as a guide.
For my collection, I thought I'd use XmlNodeList as my pattern.

Two particular things stand out:

public abstract XmlNode Item(int i);

and

public virtual XmlNode this [int index] {}

I created an indexer in my collection class without a problem. When I
added an Item() method to it, however, I was told that Item was already in
use. The debugger pointed to the indexer as the culprit. The compiler will
not continue.

So my question is this: How can I use an indexer and an Item() method in
the same class?

Thanks!

Jan 12 '06 #3
Thanks, Nicholas. That certainly makes sense.

"However, one has to ask why you would do this, since it seems you are doing
the same thing as the indexer, so why not just let the indexer do what it
does?"

My understanding is that indexers are a feature of the C# language.
Therefore, if someone used my library while developing an application in
VB.NET, they couldn't use the indexer. Providing an Item() method would
enable them the functionality. Of course, I could be completely wrong (and
it certainly would be nice to know if I am!).
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eS**************@TK2MSFTNGP10.phx.gbl...
Jeremy,

By default, the indexer of a class will be represented as an Item
method with the same parameter list and return type. If you look at the
IL for an indexer (represented here with an int parameter and an int
return type), you will see that it is represented as a property with the
name "Item":

.property int32 Item
{
.get instance int32
ConsoleApplication79.Program::get_Item(int32)
}
Also, you will notice there is an attribute on the class level:

.custom instance void
[mscorlib]System.Reflection.DefaultMemberAttribute::.ctor(st ring) = (
string("Item") )

This indicates that the Item property is the default member (indexer).

Now, if you want to have an Item method, and an indexer, then you have
to attribute the indexer with the IndexerName attribute (located in the
System.Runtime.CompilerServices namespace), indicating what name you want
your indexer to have.

However, one has to ask why you would do this, since it seems you are
doing the same thing as the indexer, so why not just let the indexer do
what it does?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:u2*************@tk2msftngp13.phx.gbl...
I am writing a collection. I downloaded the Rotor kit and started looking
at how the classes in System.Xml are written and am using them as a guide.
For my collection, I thought I'd use XmlNodeList as my pattern.

Two particular things stand out:

public abstract XmlNode Item(int i);

and

public virtual XmlNode this [int index] {}

I created an indexer in my collection class without a problem. When I
added an Item() method to it, however, I was told that Item was already
in use. The debugger pointed to the indexer as the culprit. The compiler
will not continue.

So my question is this: How can I use an indexer and an Item() method in
the same class?

Thanks!


Jan 12 '06 #4
Jeremy,

Well, have you tried to use your class from VB.NET? You will find that
the Item property should be there already. =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thanks, Nicholas. That certainly makes sense.

"However, one has to ask why you would do this, since it seems you are
doing the same thing as the indexer, so why not just let the indexer do
what it does?"

My understanding is that indexers are a feature of the C# language.
Therefore, if someone used my library while developing an application in
VB.NET, they couldn't use the indexer. Providing an Item() method would
enable them the functionality. Of course, I could be completely wrong (and
it certainly would be nice to know if I am!).
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eS**************@TK2MSFTNGP10.phx.gbl...
Jeremy,

By default, the indexer of a class will be represented as an Item
method with the same parameter list and return type. If you look at the
IL for an indexer (represented here with an int parameter and an int
return type), you will see that it is represented as a property with the
name "Item":

.property int32 Item
{
.get instance int32
ConsoleApplication79.Program::get_Item(int32)
}
Also, you will notice there is an attribute on the class level:

.custom instance void
[mscorlib]System.Reflection.DefaultMemberAttribute::.ctor(st ring) = (
string("Item") )

This indicates that the Item property is the default member (indexer).

Now, if you want to have an Item method, and an indexer, then you have
to attribute the indexer with the IndexerName attribute (located in the
System.Runtime.CompilerServices namespace), indicating what name you want
your indexer to have.

However, one has to ask why you would do this, since it seems you are
doing the same thing as the indexer, so why not just let the indexer do
what it does?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:u2*************@tk2msftngp13.phx.gbl...
I am writing a collection. I downloaded the Rotor kit and started looking
at how the classes in System.Xml are written and am using them as a
guide. For my collection, I thought I'd use XmlNodeList as my pattern.

Two particular things stand out:

public abstract XmlNode Item(int i);

and

public virtual XmlNode this [int index] {}

I created an indexer in my collection class without a problem. When I
added an Item() method to it, however, I was told that Item was already
in use. The debugger pointed to the indexer as the culprit. The compiler
will not continue.

So my question is this: How can I use an indexer and an Item() method in
the same class?

Thanks!



Jan 12 '06 #5
Sweet! Thanks a bunch, Nicholas!
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uH**************@TK2MSFTNGP14.phx.gbl...
Jeremy,

Well, have you tried to use your class from VB.NET? You will find that
the Item property should be there already. =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thanks, Nicholas. That certainly makes sense.

"However, one has to ask why you would do this, since it seems you are
doing the same thing as the indexer, so why not just let the indexer do
what it does?"

My understanding is that indexers are a feature of the C# language.
Therefore, if someone used my library while developing an application in
VB.NET, they couldn't use the indexer. Providing an Item() method would
enable them the functionality. Of course, I could be completely wrong
(and it certainly would be nice to know if I am!).
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eS**************@TK2MSFTNGP10.phx.gbl...
Jeremy,

By default, the indexer of a class will be represented as an Item
method with the same parameter list and return type. If you look at the
IL for an indexer (represented here with an int parameter and an int
return type), you will see that it is represented as a property with the
name "Item":

.property int32 Item
{
.get instance int32
ConsoleApplication79.Program::get_Item(int32)
}
Also, you will notice there is an attribute on the class level:

.custom instance void
[mscorlib]System.Reflection.DefaultMemberAttribute::.ctor(st ring) = (
string("Item") )

This indicates that the Item property is the default member
(indexer).

Now, if you want to have an Item method, and an indexer, then you
have to attribute the indexer with the IndexerName attribute (located in
the System.Runtime.CompilerServices namespace), indicating what name you
want your indexer to have.

However, one has to ask why you would do this, since it seems you are
doing the same thing as the indexer, so why not just let the indexer do
what it does?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jeremy McPeak" <jw******@gmail.com> wrote in message
news:u2*************@tk2msftngp13.phx.gbl...
I am writing a collection. I downloaded the Rotor kit and started
looking at how the classes in System.Xml are written and am using them
as a guide. For my collection, I thought I'd use XmlNodeList as my
pattern.

Two particular things stand out:

public abstract XmlNode Item(int i);

and

public virtual XmlNode this [int index] {}

I created an indexer in my collection class without a problem. When I
added an Item() method to it, however, I was told that Item was already
in use. The debugger pointed to the indexer as the culprit. The
compiler will not continue.

So my question is this: How can I use an indexer and an Item() method
in the same class?

Thanks!



Jan 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Sergey Klementiev | last post: by
2 posts views Thread by Iain | last post: by
1 post views Thread by mdub317 | last post: by
3 posts views Thread by JMMB | last post: by
3 posts views Thread by Benssol | last post: by
2 posts views Thread by AliR \(VC++ MVP\) | last post: by
reply views Thread by rosydwin | last post: by

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.