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
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