Indexer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Negrelli

    #1

    Indexer question

    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("Inde x outside the range of
    valid values.");
    else
    return (Clown)Clowns[index];
    }
    set {
    if (index < 0 || index >= Clowns.Count)
    throw new Exception("Inde x 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
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Indexer question

    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]
    - mvp@spam.guard. caspershouse.co m

    "Jerry Negrelli" <jerry.negrelli @nospam.datasci entific.com> wrote in message
    news:107d01c3a2 23$7b3323d0$a60 1280a@phx.gbl.. .[color=blue]
    > 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("Inde x outside the range of
    > valid values.");
    > else
    > return (Clown)Clowns[index];
    > }
    > set {
    > if (index < 0 || index >= Clowns.Count)
    > throw new Exception("Inde x 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[/color]


    Comment

    • Jerry Negrelli

      #3
      Re: Indexer question

      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)ClownCa r.Clowns[0]).CanJuggle = true;


      ps- there was a typo in my "get" accessor, "Clown[i]"
      should have been "Clowns[i]", so hopefully that did not
      create any confusion.
      [color=blue]
      >-----Original Message-----
      >Jerry,
      >
      > If you are providing lookup functionality, then you[/color]
      will want to derive[color=blue]
      >your class from the DictionaryBase class. It will[/color]
      provide a good deal of[color=blue]
      >the functionality for constructs of this type.
      >
      > However, it looks like what you want is Collection[/color]
      functionality,[color=blue]
      >because you are not keying on any sort of key, but[/color]
      rather an index in the[color=blue]
      >collection. Because of this, you should extend from[/color]
      CollectionBase.[color=blue]
      >
      > Clown is defined as a class, so when you do[/color]
      something like this for your[color=blue]
      >colletion:
      >
      >ClownCar[0].CanJuggle = true;
      >
      > It should work, because ClownCar is returning a[/color]
      reference type.[color=blue]
      >However, the behavior you are describing would occur if[/color]
      Clown was a[color=blue]
      >structure (value type).
      >
      > Hope this helps.
      >
      >
      >--
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >"Jerry Negrelli"[/color]
      <jerry.negrelli @nospam.datasci entific.com> wrote in
      message[color=blue]
      >news:107d01c3a 223$7b3323d0$a6 01280a@phx.gbl. ..[color=green]
      >> 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[/color][/color]
      understand[color=blue][color=green]
      >> why (a value is returned by my indexer, not a[/color][/color]
      reference).[color=blue][color=green]
      >> 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("Inde x outside the range of
      >> valid values.");
      >> else
      >> return (Clown)Clowns[index];
      >> }
      >> set {
      >> if (index < 0 || index >= Clowns.Count)
      >> throw new Exception("Inde x 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[/color]
      >
      >
      >.
      >[/color]

      Comment

      Working...