Indexer question

J

Jerry Negrelli

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[index] instead of
Class1.someVar[index].

The following code does not work & I believe I understand
why (a value is returned by my indexer, not a reference).
I have a feeling I'm just using this indexer horribly
wrong, but any insight would be much appreciated!

//Class 1 definition
class ClownCar{
private Hashtable Clowns = new Hashtable();

//...code omitted

//Properties
public Clown this [int index] {
get {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Index outside the range of
valid values.");
else
return (Clown)Clowns[index];
}
set {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Index outside the range of
valid values.");
else
Clown[index] = value;
}
}
}


//Class 2 definition
class Clown{
bool CanJuggle = true;
//...code omitted
}


//client script tries to set an indexer's data

ClownCar RedClownCar = new ClownCar();

//...code omitted

//the following line of code does jack sh*t to the
referenced object (i.e. varClass[0]'s internal values
will remain the same).

RedClownCar[0].CanJuggle= false;


Jerry Negrelli
 
N

Nicholas Paldino [.NET/C# MVP]

Jerry,

If you are providing lookup functionality, then you will want to derive
your class from the DictionaryBase class. It will provide a good deal of
the functionality for constructs of this type.

However, it looks like what you want is Collection functionality,
because you are not keying on any sort of key, but rather an index in the
collection. Because of this, you should extend from CollectionBase.

Clown is defined as a class, so when you do something like this for your
colletion:

ClownCar[0].CanJuggle = true;

It should work, because ClownCar is returning a reference type.
However, the behavior you are describing would occur if Clown was a
structure (value type).

Hope this helps.
 
J

Jerry Negrelli

Clown is definitely a class, not a struct, so shouldn't
the following two commands yield the same result? Does
indexing the ClownCar class interfere with this?

//does not work
ClownCar[0].CanJuggle = true;

//works, if I simply change the "Clowns" Hashtable
to "public"
((Clown)ClownCar.Clowns[0]).CanJuggle = true;


ps- there was a typo in my "get" accessor, "Clown"
should have been "Clowns", so hopefully that did not
create any confusion.
-----Original Message-----
Jerry,

If you are providing lookup functionality, then you will want to derive
your class from the DictionaryBase class. It will provide a good deal of
the functionality for constructs of this type.

However, it looks like what you want is Collection functionality,
because you are not keying on any sort of key, but rather an index in the
collection. Because of this, you should extend from CollectionBase.

Clown is defined as a class, so when you do something like this for your
colletion:

ClownCar[0].CanJuggle = true;

It should work, because ClownCar is returning a reference type.
However, the behavior you are describing would occur if Clown was a
structure (value type).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Jerry Negrelli"
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[index] instead of
Class1.someVar[index].

The following code does not work & I believe I understand
why (a value is returned by my indexer, not a reference).
I have a feeling I'm just using this indexer horribly
wrong, but any insight would be much appreciated!

//Class 1 definition
class ClownCar{
private Hashtable Clowns = new Hashtable();

//...code omitted

//Properties
public Clown this [int index] {
get {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Index outside the range of
valid values.");
else
return (Clown)Clowns[index];
}
set {
if (index < 0 || index >= Clowns.Count)
throw new Exception("Index outside the range of
valid values.");
else
Clowns[index] = value;
}
}
}


//Class 2 definition
class Clown{
bool CanJuggle = true;
//...code omitted
}


//client script tries to set an indexer's data

ClownCar RedClownCar = new ClownCar();

//...code omitted

//the following line of code does jack sh*t to the
referenced object (i.e. varClass[0]'s internal values
will remain the same).

RedClownCar[0].CanJuggle= false;


Jerry Negrelli


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top