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

What Is an Indexer?

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,
Jun 19 '06 #1
17 2041
SemSem wrote:
i want to know waht is an index and how we use it with a simple
example including the main of the program .


An indexer is a pseudo-property that allows an object to be accessed like an
array.

<code>
using System;

public class Example
{
int this[int index]
{
get { return index; }
set { Console.WriteLine("Set: {0}",value); }
}

static void Main()
{
Example e = new Example();
Console.WriteLine("e[5]={0}",e[5]);
e[6] = 42;
}
}

</code>

This is, of course, a completely contrived and useless example, but it
illustrates how, in Main(), the object e is accessed as if it were an array
of integers. Note that the getter and setter for the indexer need not
implement behavior that's even remotely like that of an array, although
generally it's a good idea to only use an indexer for truly array-like
behavior.

Note that an object can have any number of indexers, and that the "indexes"
of the pseudo-array can have any type and there can be any number of them:

<code>
using System;

public class Example2
{
string this[string s1, string s2, string s3]
{
get { return string.Format("{0}:{1}:{2}",s1,s2,s3); }
set { Console.WriteLine("{0}:{1}:{2}={3}",s1,s2,s3,value ); }
}

static void Main()
{
Example2 e2 = new Example2();
Console.WriteLine("{0}",e2["one","two","three"]);
e2["hello","world","!!"] = "Wow!";
}
}
</code>

-cd
Jun 19 '06 #2
"SemSem" <Se****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
i want to know waht is an index and how we use it with a simple example
including the main of the program .


You should change majors.
Jun 19 '06 #3
Wow, you are so helpful. You really are the type of person that should be
responding to questions posted here.

Thanks for all your invaluable help!
"Mark Wilden" <Ma********@newsgroups.nospam> wrote in message
news:uE****************@TK2MSFTNGP05.phx.gbl...
"SemSem" <Se****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
i want to know waht is an index and how we use it with a simple example
including the main of the program .


You should change majors.

Jun 20 '06 #4
Isn't is more to allow one data value to be treated like an array? In other
words, the bit representation of a given number may be 01100110000011111,
but instead of getting the Int16 value of this bit stream, you could treat
this like a Boolean array of 16 values?

How would/could you use this with traditional objects rather than data
types?
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
SemSem wrote:
i want to know waht is an index and how we use it with a simple
example including the main of the program .


An indexer is a pseudo-property that allows an object to be accessed like
an array.

<code>
using System;

public class Example
{
int this[int index]
{
get { return index; }
set { Console.WriteLine("Set: {0}",value); }
}

static void Main()
{
Example e = new Example();
Console.WriteLine("e[5]={0}",e[5]);
e[6] = 42;
}
}

</code>

This is, of course, a completely contrived and useless example, but it
illustrates how, in Main(), the object e is accessed as if it were an
array of integers. Note that the getter and setter for the indexer need
not implement behavior that's even remotely like that of an array,
although generally it's a good idea to only use an indexer for truly
array-like behavior.

Note that an object can have any number of indexers, and that the
"indexes" of the pseudo-array can have any type and there can be any
number of them:

<code>
using System;

public class Example2
{
string this[string s1, string s2, string s3]
{
get { return string.Format("{0}:{1}:{2}",s1,s2,s3); }
set { Console.WriteLine("{0}:{1}:{2}={3}",s1,s2,s3,value ); }
}

static void Main()
{
Example2 e2 = new Example2();
Console.WriteLine("{0}",e2["one","two","three"]);
e2["hello","world","!!"] = "Wow!";
}
}
</code>

-cd

Jun 20 '06 #5
Scott M. wrote:
Isn't is more to allow one data value to be treated like an array? In
other words, the bit representation of a given number may be
01100110000011111, but instead of getting the Int16 value of this bit
stream, you could treat this like a Boolean array of 16 values?
You could use an indexer for that, sure. But then consider a
Dictionary<string,int>. The indexer makes this type behave as-if it were an
array of integers indexed by strings (the inverse of string[] in other
words).

How would/could you use this with traditional objects rather than data
types?


In your conceptual framework, what distinguishes a "data type" from a
"traditional object"?

Indexes can be defined on any class or struct, regardless of the intended
purpose. I certainly wouldn't recommend using an indexer on an object that
doesn't have some sort of array-like, or map-like feel to it, but there's an
awful lot of possibilities within those restrictions.

-cd
Jun 20 '06 #6
ithink iam here to have helped not to be disaponieted
Jun 20 '06 #7
> In your conceptual framework, what distinguishes a "data type" from a
"traditional object"?
Struct vs. Class
Indexes can be defined on any class or struct, regardless of the intended
purpose. I certainly wouldn't recommend using an indexer on an object
that doesn't have some sort of array-like, or map-like feel to it, but
there's an awful lot of possibilities within those restrictions.


So, how could an indexer help me with, say a CheckBox class, or a
DataAdapter class?

Thanks,

Scott
Jun 20 '06 #8
Scott M. <s-***@nospam.nospam> wrote:
In your conceptual framework, what distinguishes a "data type" from a
"traditional object"?


Struct vs. Class


That's a dangerous way of looking at it - in C# terms at least. "Data
type" vs "Traditional object" doesn't in any way capture "value type"
vs "reference type" IMO, and that's the crux of the difference between
structs and classes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '06 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Scott M. <s-***@nospam.nospam> wrote:
> In your conceptual framework, what distinguishes a "data type" from a
> "traditional object"?


Struct vs. Class


That's a dangerous way of looking at it - in C# terms at least. "Data
type" vs "Traditional object" doesn't in any way capture "value type"
vs "reference type" IMO, and that's the crux of the difference between
structs and classes.


To each his own. Looking at things as either a Data Type (Structure / Value
Type) or Traditional Object (Class / Reference Type), works fine for me
because I understand the difference between a ref type and a val type.

Your response doesn't answer my question though.
Jun 21 '06 #10
Scott M. <s-***@nospam.nospam> wrote:
That's a dangerous way of looking at it - in C# terms at least. "Data
type" vs "Traditional object" doesn't in any way capture "value type"
vs "reference type" IMO, and that's the crux of the difference between
structs and classes.
To each his own. Looking at things as either a Data Type (Structure / Value
Type) or Traditional Object (Class / Reference Type), works fine for me
because I understand the difference between a ref type and a val type.


What does a C++ class count as then? C++ classes aren't reference types
(in themselves - obviously you can use pointers), but surely they count
as "traditional objects" don't they? The difference between a struct
and a class is well documented - could you either give a clearer
definition of what a "data type" is compared with a "traditional
object" or link to an online resource with such a definition?
Your response doesn't answer my question though.


No, but I'm not entirely sure what your question really means. A
Hashtable is a class, and thus a "traditional object" by your
definition - I hope you see where Hashtable's indexer is useful...

Indexers are usually most commonly used in collections, but I don't see
that the struct vs class distinction is relevant to that, and indeed
I've rarely if ever seen a collection which is a value type in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '06 #11
> What does a C++ class count as then? C++ classes aren't reference types
(in themselves - obviously you can use pointers), but surely they count
as "traditional objects" don't they?
I don't know, nor do I care since I don't use C++ in any way.
The difference between a struct
and a class is well documented - could you either give a clearer
definition of what a "data type" is compared with a "traditional
object" or link to an online resource with such a definition?


I just did in my last post:

Data Type (Structure / Value Type)
Traditional Object (Class / Reference Type)

I see nothing ambiguous or unclear with this.
Your response doesn't answer my question though.


No, but I'm not entirely sure what your question really means. A
Hashtable is a class, and thus a "traditional object" by your
definition - I hope you see where Hashtable's indexer is useful...


I think you are getting too hung up on minutia. The thread is about
indexers. My question was about a CheckBox class, or a
DataAdapter class. Just because what I call something isn't what you would
call it is irrelevant to the question. It is apparent that you understand
what my terminology means, so could you just get to the point?

I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case? And if so, could you explain how?

Thanks.

Jun 21 '06 #12

Scott M. wrote:
I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case?


Your question is a little malformed, but essentially, yes. An indexer
is a member, very similar to a property, that takes an index as an
argument. It's declared using the 'this' keyword. Hashtable has an
indexer, and you can create them on any of your classes or structs.
Here is a possible Hashtable indexer implementation:

public object this [Object key] {
get {
object o = Find(key);
return o;
}
set {
Put(key, value);
}
}

When you call
object o = hash["foo"]
you are calling the Hashtable indexer.

MSDN is your friend.
http://msdn.microsoft.com/library/en...opertiesPG.asp

Jun 22 '06 #13

mpetro...@gmail.com wrote:
Scott M. wrote:
I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case?


Your question is a little malformed, but essentially, yes. An indexer
is a member, very similar to a property, that takes an index as an
argument. It's declared using the 'this' keyword. Hashtable has an
indexer, and you can create them on any of your classes or structs.
Here is a possible Hashtable indexer implementation:


This might also help: indexers can be applied to any class (or struct,
or interface), not just collection classes. For instance:

public string this[int index]
{
get
{
if (index < 12)
return "Good morning!";
else if (index < 18)
return "Good afternoon!";
else
return "Good evening!";
}
//...
}

Doesn't mean you *should* use it like this, mind.

Jun 22 '06 #14
Scott M. <s-***@nospam.nospam> wrote:
The difference between a struct
and a class is well documented - could you either give a clearer
definition of what a "data type" is compared with a "traditional
object" or link to an online resource with such a definition?
I just did in my last post:

Data Type (Structure / Value Type)
Traditional Object (Class / Reference Type)

I see nothing ambiguous or unclear with this.


Okay, we'll go along with it. You should be aware that it's terminology
which is far from standard though. (I haven't seen anyone else use it.)
Your response doesn't answer my question though.


No, but I'm not entirely sure what your question really means. A
Hashtable is a class, and thus a "traditional object" by your
definition - I hope you see where Hashtable's indexer is useful...


I think you are getting too hung up on minutia. The thread is about
indexers. My question was about a CheckBox class, or a
DataAdapter class. Just because what I call something isn't what you would
call it is irrelevant to the question. It is apparent that you understand
what my terminology means, so could you just get to the point?


Sure. You asked whether indexers would/could be used with "traditional
objects" (i.e. reference types). I've given an example of where
indexers *are* used in reference types.
I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an indexer
in this case? And if so, could you explain how?


The index passed to the Hashtable's indexer isn't the indexer itself -
the index is just the value of the argument. In the case of Hashtable,
that's a reference, so if you meant the index rather than the indexer,
it still provides an example. For instance:

Hashtable x = new Hashtable();
x["hello"] = "there";

The indexer is the member (or rather, the pair of members) which allows
you to use the [] syntax. There's nothing special about the value in
the brackets being a reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 22 '06 #15
Okay, we'll go along with it. You should be aware that it's terminology
which is far from standard though. (I haven't seen anyone else use it.)
You should be aware that there *are* people who use this terminology and,
allthough it may not be standard jargon in your travels, it is in mine. In
fact, the separation of an object from a data type was standard operating
procedure in the VB 6.0 world. In that universe, a Boolean (for example)
was not treated as an object, it was treated as a data type. VB 6.0 even
went so far as to have a special keyword (Set) to differentiate between
referring to data types vs. objects.

As I said before, I think you should let the fact that I didn't phrase my
question using the verbiage you would have used go. I get the feeling that
you knew what I meant all along.
Sure. You asked whether indexers would/could be used with "traditional
objects" (i.e. reference types). I've given an example of where
indexers *are* used in reference types.
My question was:

"So, how could an indexer help me with, say a CheckBox class, or a
DataAdapter class?"
I do see how an index is useful with a Hashtable or any other collection
class, but is this index passed to a Hashtable the same thing as an
indexer
in this case? And if so, could you explain how?


The index passed to the Hashtable's indexer isn't the indexer itself -
the index is just the value of the argument. In the case of Hashtable,
that's a reference, so if you meant the index rather than the indexer,
it still provides an example. For instance:

Hashtable x = new Hashtable();
x["hello"] = "there";

The indexer is the member (or rather, the pair of members) which allows
you to use the [] syntax. There's nothing special about the value in
the brackets being a reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 22 '06 #16
Scott M. <s-***@nospam.nospam> wrote:
Okay, we'll go along with it. You should be aware that it's terminology
which is far from standard though. (I haven't seen anyone else use it.)
You should be aware that there *are* people who use this terminology and,
allthough it may not be standard jargon in your travels, it is in mine. In
fact, the separation of an object from a data type was standard operating
procedure in the VB 6.0 world. In that universe, a Boolean (for example)
was not treated as an object, it was treated as a data type. VB 6.0 even
went so far as to have a special keyword (Set) to differentiate between
referring to data types vs. objects.


As you pointed out that C# isn't C++, the same should be said here - C#
isn't VB 6.0, and it certainly isn't C# terminology, nor do I see that
it's useful terminology. Indeed, we've already seen that it causes
confusion.
As I said before, I think you should let the fact that I didn't phrase my
question using the verbiage you would have used go. I get the feeling that
you knew what I meant all along.


I really, really didn't. (And neither did Carl, clearly.)
Sure. You asked whether indexers would/could be used with "traditional
objects" (i.e. reference types). I've given an example of where
indexers *are* used in reference types.


My question was:

"So, how could an indexer help me with, say a CheckBox class, or a
DataAdapter class?"


And it doesn't, particularly. The same is true with overloading the ==
operator, which doesn't mean it's not useful in other classes. String
is a good example of another class which has an indexer - the indexer
returns the character at the given index, eg "hello"[3]=='l'.

It doesn't make sense to have indexers on *all* classes, but there are
plenty where it does make sense. Anything where it makes sense to ask
what the nth "element" (whatever that means for that class) would make
sense in terms of an integer indexer, and wherever there's a map
concept it may well make sense to index on other types such as strings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 22 '06 #17
> It doesn't make sense to have indexers on *all* classes, but there are
plenty where it does make sense. Anything where it makes sense to ask
what the nth "element" (whatever that means for that class) would make
sense in terms of an integer indexer, and wherever there's a map
concept it may well make sense to index on other types such as strings.


Thanks. That's all I was really looking for.
Jun 22 '06 #18

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
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
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...
2
by: Jerry Negrelli | last post by:
I'm trying to define a custom class indexer to take the place of an underlying Hashtable member so that values can be referred to as Class1 instead of Class1.someVar. The following code does...
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...
18
by: Jm | last post by:
Hi all I feel stupid for asking this, but i just went to use the left() function from vb6 only to find it doesnt do what it used to under .NET. Im assuming theres something else now im meant to...
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 { // ...
3
by: Nash Alexx | last post by:
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...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.