473,388 Members | 1,375 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,388 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 1562
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
2
by: Iain | last post by:
I'm writing some reflection code which resolves the value of a complex expression (e.g. MyInfo.Customers.Orders.Date), against a specific object. I'm struggling a bit with indexers. I'm not...
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
5
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know...
3
by: JMMB | last post by:
Are there Indexers in VB.Net the same way they exist in C#? thanks,
0
by: liko81 | last post by:
I have an Invoice class that must know, directly or indirectly, how to do anything associated with creating, reading, or otherwise processing an invoice to a customer. It is an uber-DAO object that...
3
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
2
by: AliR \(VC++ MVP\) | last post by:
Hi Everyone, I have a class that has two instance variables that are Dictionaries. Now in addition to having properties to access these dicitionaries, I wanted to have a properties. But it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.