Subclassing ArrayList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guinness Mann

    #1

    Subclassing ArrayList

    I have a class that I use throughout my application, and I store
    collections of my class in an ArrayList. To avoid some really ugly
    casting I'd like to subclass ArrayList to get a typed version.

    Has anyone done this? Do you have any advice for me? How about some
    sample code?


    --
    Who's better for furniture? NOOOOOOOOOOOOOO OOOOBODY!!!!

    Guinness Mann
    GMann@Dublin.co m

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Subclassing ArrayList

    Guiness Mann,

    Instead of subclassing ArrayList, you should extend the CollectionBase
    class, as it is the base for expandable collections which are type safe.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Guinness Mann" <GMann@dublin.c om> wrote in message
    news:MPG.19ecaf 507f39e5ef9896e 8@news.newsguy. com...[color=blue]
    > I have a class that I use throughout my application, and I store
    > collections of my class in an ArrayList. To avoid some really ugly
    > casting I'd like to subclass ArrayList to get a typed version.
    >
    > Has anyone done this? Do you have any advice for me? How about some
    > sample code?
    >
    >
    > --
    > Who's better for furniture? NOOOOOOOOOOOOOO OOOOBODY!!!!
    >
    > Guinness Mann
    > GMann@Dublin.co m
    >[/color]


    Comment

    • Guinness Mann

      #3
      Re: Subclassing ArrayList

      In article <ezA#YOQjDHA.11 68@tk2msftngp13 .phx.gbl>,
      nicholas.paldin o@exisconsultin g.com says...
      [color=blue]
      > Instead of subclassing ArrayList, you should extend the CollectionBase
      > class, as it is the base for expandable collections which are type safe.[/color]

      I'd be interested in seeing some sample code. In the meantime, I've
      done this:

      public class QRowArray : ArrayList
      {
      public new QRow this[int i]
      {
      get { return (QRow)base[i]; }
      set { base[i] = value; }
      }
      }

      Perhaps you could point out to me the errors of my ways?

      -- Rick

      public class QRow : IComparable
      {
      int numDistractors = Convert.ToInt32
      (UA.DistractorI nfo.NUM_DISTRAC TORS);

      public Distractor question;
      public Distractor[] dis;
      public int answer;
      public int placement;

      public QRow()
      {
      dis = new Distractor[numDistractors];
      answer = 0;
      placement = -1;
      }
      #region IComparable Members

      public int CompareTo(objec t rhs)
      {
      QRow r = (QRow) rhs;
      return this.placement. CompareTo(r.pla cement);
      }

      #endregion
      }

      public class Distractor
      {
      public int dId;
      public string text;

      public Distractor()
      {
      dId = 0;
      text = "";
      }
      }

      --

      --
      Who's better for furniture? NOOOOOOOOOOOOOO OOOOBODY!!!!

      Guinness Mann
      GMann@Dublin.co m

      Comment

      • 100

        #4
        Re: Subclassing ArrayList

        Hi,
        This is the way I use CollectionBase when I want to create a strongly typed
        collection

        class MyList: CollectionBase
        {
        public QRow this[int index]
        {
        get{return (QRow)InnerList[index];}
        set{InnerList[index] = value;}
        }

        public int Add(QRow value)
        {
        return InnerList.Add(v alue);
        }

        public void Remove(QRow value)
        {
        InnerList.Remov e(value);
        }

        protected override void OnValidate(obje ct value)
        {
        base.OnValidate (value); //if null is acceptable don't call base
        implemetation
        if(!(value is QRow)) throw new ArgumentExcepti on("Type of the
        value is not acceptable");
        }

        }

        B\rgds
        100

        "Guinness Mann" <GMann@dublin.c om> wrote in message
        news:MPG.19ecc6 edb0239df99896e c@news.newsguy. com...[color=blue]
        > In article <ezA#YOQjDHA.11 68@tk2msftngp13 .phx.gbl>,
        > nicholas.paldin o@exisconsultin g.com says...
        >[color=green]
        > > Instead of subclassing ArrayList, you should extend the[/color][/color]
        CollectionBase[color=blue][color=green]
        > > class, as it is the base for expandable collections which are type safe.[/color]
        >
        > I'd be interested in seeing some sample code. In the meantime, I've
        > done this:
        >
        > public class QRowArray : ArrayList
        > {
        > public new QRow this[int i]
        > {
        > get { return (QRow)base[i]; }
        > set { base[i] = value; }
        > }
        > }
        >
        > Perhaps you could point out to me the errors of my ways?
        >
        > -- Rick
        >
        > public class QRow : IComparable
        > {
        > int numDistractors = Convert.ToInt32
        > (UA.DistractorI nfo.NUM_DISTRAC TORS);
        >
        > public Distractor question;
        > public Distractor[] dis;
        > public int answer;
        > public int placement;
        >
        > public QRow()
        > {
        > dis = new Distractor[numDistractors];
        > answer = 0;
        > placement = -1;
        > }
        > #region IComparable Members
        >
        > public int CompareTo(objec t rhs)
        > {
        > QRow r = (QRow) rhs;
        > return this.placement. CompareTo(r.pla cement);
        > }
        >
        > #endregion
        > }
        >
        > public class Distractor
        > {
        > public int dId;
        > public string text;
        >
        > public Distractor()
        > {
        > dId = 0;
        > text = "";
        > }
        > }
        >
        > --
        >
        > --
        > Who's better for furniture? NOOOOOOOOOOOOOO OOOOBODY!!!!
        >
        > Guinness Mann
        > GMann@Dublin.co m
        >[/color]


        Comment

        • Guinness Mann

          #5
          Re: Subclassing ArrayList

          Hey, thanks, 100. I've copied your example into my snippets for
          possible use on another project. In this case, though, I really want to
          use ArrayList because of some of it's member functions (like Sort), and
          it seems incredibly easy to simply add this:

          public class QRowArray : ArrayList
          {
          public new QRow this[int i]
          {
          get { return (QRow)base[i]; }
          set { base[i] = value; }
          }
          }

          And be done with it. I understand that I, for instance, don't have type
          safety on input data, but it's really just convenience code to keep all
          the ugly casting out of the body of my code; it's not production code.
          Have I violated any secret laws?

          -- Rick



          In article <ehEWmVRjDHA.13 32@tk2msftngp13 .phx.gbl>, 100@100.com says...[color=blue]
          > Hi,
          > This is the way I use CollectionBase when I want to create a strongly typed
          > collection
          >
          > class MyList: CollectionBase
          > {
          > public QRow this[int index]
          > {
          > get{return (QRow)InnerList[index];}
          > set{InnerList[index] = value;}
          > }
          >
          > public int Add(QRow value)
          > {
          > return InnerList.Add(v alue);
          > }
          >
          > public void Remove(QRow value)
          > {
          > InnerList.Remov e(value);
          > }
          >
          > protected override void OnValidate(obje ct value)
          > {
          > base.OnValidate (value); //if null is acceptable don't call base
          > implemetation
          > if(!(value is QRow)) throw new ArgumentExcepti on("Type of the
          > value is not acceptable");
          > }
          >
          > }[/color]

          Comment

          • 100

            #6
            Re: Subclassing ArrayList

            Hi Guinness,
            Here is my new class supporting all sort that ArrayList has + type safety.

            class MyList: CollectionBase
            {
            public ArrayList mInnerArrayList ;

            public MyList()
            {
            mInnerArrayList = ArrayList.Adapt er(this.InnerLi st);
            }

            public QRow this[int index]
            {
            get{return (QRow)InnerList[index];}
            set{InnerList[index] = value;}
            }

            public int Add(QRow value)
            {
            return InnerList.Add(v alue);
            }

            public void Remove(QRow value)
            {
            InnerList.Remov e(value);
            }

            public void Sort()
            {
            mInnerArrayList .Sort();
            }

            protected override void OnValidate(obje ct value)
            {
            base.OnValidate (value);
            if(!(value is QRow)) throw new ArgumentExcepti on("Type of the
            value is not acceptable");
            }
            }

            You can add the second overload of the *sort* method as well as other
            ArrayList operations that you miss in CollectionBase.

            HTH
            B\rgds
            100

            "Guinness Mann" <GMann@dublin.c om> wrote in message
            news:MPG.19ece3 57221679f99896f 1@news.newsguy. com...[color=blue]
            > Hey, thanks, 100. I've copied your example into my snippets for
            > possible use on another project. In this case, though, I really want to
            > use ArrayList because of some of it's member functions (like Sort), and
            > it seems incredibly easy to simply add this:
            >
            > public class QRowArray : ArrayList
            > {
            > public new QRow this[int i]
            > {
            > get { return (QRow)base[i]; }
            > set { base[i] = value; }
            > }
            > }
            >
            > And be done with it. I understand that I, for instance, don't have type
            > safety on input data, but it's really just convenience code to keep all
            > the ugly casting out of the body of my code; it's not production code.
            > Have I violated any secret laws?
            >
            > -- Rick
            >
            >
            >
            > In article <ehEWmVRjDHA.13 32@tk2msftngp13 .phx.gbl>, 100@100.com says...[color=green]
            > > Hi,
            > > This is the way I use CollectionBase when I want to create a strongly[/color][/color]
            typed[color=blue][color=green]
            > > collection
            > >
            > > class MyList: CollectionBase
            > > {
            > > public QRow this[int index]
            > > {
            > > get{return (QRow)InnerList[index];}
            > > set{InnerList[index] = value;}
            > > }
            > >
            > > public int Add(QRow value)
            > > {
            > > return InnerList.Add(v alue);
            > > }
            > >
            > > public void Remove(QRow value)
            > > {
            > > InnerList.Remov e(value);
            > > }
            > >
            > > protected override void OnValidate(obje ct value)
            > > {
            > > base.OnValidate (value); //if null is acceptable don't call[/color][/color]
            base[color=blue][color=green]
            > > implemetation
            > > if(!(value is QRow)) throw new ArgumentExcepti on("Type of the
            > > value is not acceptable");
            > > }
            > >
            > > }[/color][/color]


            Comment

            • Guinness Mann

              #7
              Re: Subclassing ArrayList

              You're the man, 100. Thanks again.

              --Rick


              In article <OlLz2najDHA.36 12@TK2MSFTNGP11 .phx.gbl>, 100@100.com says...[color=blue]
              > Hi Guinness,
              > Here is my new class supporting all sort that ArrayList has + type safety.
              >
              > class MyList: CollectionBase
              > {
              > public ArrayList mInnerArrayList ;
              >
              > public MyList()
              > {
              > mInnerArrayList = ArrayList.Adapt er(this.InnerLi st);
              > }
              >
              > public QRow this[int index]
              > {
              > get{return (QRow)InnerList[index];}
              > set{InnerList[index] = value;}
              > }
              >
              > public int Add(QRow value)
              > {
              > return InnerList.Add(v alue);
              > }
              >
              > public void Remove(QRow value)
              > {
              > InnerList.Remov e(value);
              > }
              >
              > public void Sort()
              > {
              > mInnerArrayList .Sort();
              > }
              >
              > protected override void OnValidate(obje ct value)
              > {
              > base.OnValidate (value);
              > if(!(value is QRow)) throw new ArgumentExcepti on("Type of the
              > value is not acceptable");
              > }
              > }
              >
              > You can add the second overload of the *sort* method as well as other
              > ArrayList operations that you miss in CollectionBase.
              >
              > HTH
              > B\rgds
              > 100
              >
              > "Guinness Mann" <GMann@dublin.c om> wrote in message
              > news:MPG.19ece3 57221679f99896f 1@news.newsguy. com...[/color]

              Comment

              • 100

                #8
                Re: Subclassing ArrayList

                :-D You are very welcome!

                Take care
                100

                "Guinness Mann" <GMann@dublin.c om> wrote in message
                news:MPG.19ede2 b46c86c7899896f 3@news.newsguy. com...[color=blue]
                > You're the man, 100. Thanks again.
                >
                > --Rick
                >
                >
                > In article <OlLz2najDHA.36 12@TK2MSFTNGP11 .phx.gbl>, 100@100.com says...[color=green]
                > > Hi Guinness,
                > > Here is my new class supporting all sort that ArrayList has + type[/color][/color]
                safety.[color=blue][color=green]
                > >
                > > class MyList: CollectionBase
                > > {
                > > public ArrayList mInnerArrayList ;
                > >
                > > public MyList()
                > > {
                > > mInnerArrayList = ArrayList.Adapt er(this.InnerLi st);
                > > }
                > >
                > > public QRow this[int index]
                > > {
                > > get{return (QRow)InnerList[index];}
                > > set{InnerList[index] = value;}
                > > }
                > >
                > > public int Add(QRow value)
                > > {
                > > return InnerList.Add(v alue);
                > > }
                > >
                > > public void Remove(QRow value)
                > > {
                > > InnerList.Remov e(value);
                > > }
                > >
                > > public void Sort()
                > > {
                > > mInnerArrayList .Sort();
                > > }
                > >
                > > protected override void OnValidate(obje ct value)
                > > {
                > > base.OnValidate (value);
                > > if(!(value is QRow)) throw new ArgumentExcepti on("Type of the
                > > value is not acceptable");
                > > }
                > > }
                > >
                > > You can add the second overload of the *sort* method as well as other
                > > ArrayList operations that you miss in CollectionBase.
                > >
                > > HTH
                > > B\rgds
                > > 100
                > >
                > > "Guinness Mann" <GMann@dublin.c om> wrote in message
                > > news:MPG.19ece3 57221679f99896f 1@news.newsguy. com...[/color][/color]


                Comment

                Working...