473,803 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1479
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***@discussio ns.microsoft.co m> 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
8375
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
1522
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 reflection to use the indexer property, or is there some sort of casting that I can do? I would like to do something like this.
16
1992
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 parameter types" can any one advice: can I declear more then one indexer?
7
485
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(); // ?? .... ColorCollection _colors=new ColorCollection() public ColorCollection Color
7
4621
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 than indexer Whouse.this. Would someone please tell me why? public class Whouse : IEnumerable public CRegs this // Indexer { get
5
2300
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
7514
by: Bill Cohagan | last post by:
I'm curious as to why C# doesn't support static indexers. Anybody know? Thanks, Bill
17
2093
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
1249
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 believe they are because of indexer) Does any one can give me procedure to do the above? -Cnu
0
9703
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
10317
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...
1
10300
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6844
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
5503
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
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.