473,383 Members | 1,813 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,383 software developers and data experts.

Concept of Indexer

Hello!

I am quite new to C#, and one concept that really gives me a headache is
"indexer". I have gone through the MSDN examples, and, at some level know how
to use indexers. But, the thing is I do not understand the specification that
says something along this line "with indexers you can index objects of a
class like an array". The thing is, in all the examples, there is only one
"new" call when only one object is created! There is no array of objects.
Yet, right after the "new" call, indexer is used, like obj[0], obj[1],
obj[2], ..., objNo. When these objects are instantiated? Also, I have tried
printing messages through contructor, hoping that array-like reference
through the indexer will invoke a constructor for each object in the array,
but it did not. Only once that happened when the object is first (and only
time, it appears) was created.

Any explanation? Is there arereally "n" objects created from the class that
has declared an "indexer"?

Thanks for the help!

Nash,
Toronto
May 10 '06 #1
3 1467
JT
Can you include specific code for your declaration and how you're
trying to use it? Maybe include the example code you're following,
too.

string declarations don't require any special syntax, but once assigned
can be referenced like character arrays, like in the old C and C++
days. I get the feeling you're not referring to strings though. Some
other declarations do have some weird syntax though.

May 10 '06 #2
A good example of a class using an indexer is ArrayList.

//here we create an ArrayList object, note it is only one object
ArrayList list = new ArrayList();

//note we access it like an array even though it is not
list[0] = "test";

Notice this it uses a normal constructor unlike that of an array, and
the result is you only get one object, not an array of objects.

Internally the array list may choose to use an array to store its data,
or it could use a linked list, or any other form it chooses, but this is
up to the object.

If you were to define the ArrayList like:
ArrayList lists[] = new ArrayList[5];

what you have in fact created is an array of ArrayLists, this is no
different (other than strong typing) than having defined:
object obj[] = new object[5];

And as such the array, lists[], will initialy contain nothing but null
until you assign each element a new ArrayList.

An indexer just simply allows you to use things like:
myObject[5]
myObject["test"]
etc.

As opposed to having to write two methods such as:
object GetValue(int index)
SetValue(int index, object value)

The index can be any type the class' author wishes to use, such as the
HashTable where the indexer (the key) is an object.

Likewise the return value can be anything the class' author wishes to
use.

On Tue, 9 May 2006 16:29:02 -0700, Nash Alexx <Nash
Al***@discussions.microsoft.com> wrote:
Hello!

I am quite new to C#, and one concept that really gives me a headache is
"indexer". I have gone through the MSDN examples, and, at some level know how
to use indexers. But, the thing is I do not understand the specification that
says something along this line "with indexers you can index objects of a
class like an array". The thing is, in all the examples, there is only one
"new" call when only one object is created! There is no array of objects.
Yet, right after the "new" call, indexer is used, like obj[0], obj[1],
obj[2], ..., objNo. When these objects are instantiated? Also, I have tried
printing messages through contructor, hoping that array-like reference
through the indexer will invoke a constructor for each object in the array,
but it did not. Only once that happened when the object is first (and only
time, it appears) was created.

Any explanation? Is there arereally "n" objects created from the class that
has declared an "indexer"?

Thanks for the help!

Nash,
Toronto

May 10 '06 #3
V
Hi,

Just wanted to clarify one thing (for someone who might be a newbie):
//here we create an ArrayList object, note it is only one object
ArrayList list = new ArrayList(); //note we access it like an array even though it is not
list[0] = "test";


In the above example, you will get an error thrown until unless there
are already at least 3 elements in the ArrayList (either you have added
them by calling list.Add(object) 3 times, or maybe by instantiating the
Arraylist to have at least 3 elements to begin with.

Regards,
Vaibhav

May 10 '06 #4

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

Similar topics

14
by: Nikhil Patel | last post by:
Hi all, Is it possible to have more than one indexer in a class. If not, are there any good alternative ways to achieve similar functionality. Thanks... -Nikhil
0
by: Brian Takita | last post by:
Hello, I am able to test an object to see if it is an indexer (IsIndexer function), however I don't know how to call the indexer without Unboxing to the object's class. Do I need to use...
16
by: Yoramo | last post by:
Hello I have a class the containes several ArrayList's and I whold like to use a indexer for each one. I'm getting a compilation error. "already defines a member called 'this' with the same...
7
by: Oren | last post by:
hi I have created a class with a simple indexer what is the syntax for adding the function "Remove" ... myClass.colors=Color.Black; // WORKS O myClass.colors.Remove(); // ?? .......
7
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible...
5
by: Clive Dixon | last post by:
Is it possible to access an indexer of a base class with identical signature, e.g. class Class1 { public object this { get { // ...
8
by: Bill Cohagan | last post by:
I'm curious as to why C# doesn't support static indexers. Anybody know? Thanks, Bill
17
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
3
by: Duggi | last post by:
Hello, I have a class with an indexer in the class. The code code is in C#. However I was trying to access the indexer in the other lang of .Net, VB. I ran into some indexer related issues (I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.