type-safe collection

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

    type-safe collection

    I would like to make a strongly typed, sortable collection by leveraging a
    Framework class. I began by looking at CollectionBase but it doesn't have
    built-int sorting. I would prefer to derive from ArrayList, but if I code
    an Add(MyElement) method, the original Add(object) remains exposed,
    compromising type safety. I know this must be a common task, how to do it?
    Thanks,
    Gary


  • Daniel O'Connell

    #2
    Re: type-safe collection

    CollectionBase uses an array list internally(the protected InnerList
    property). You should be able to simply call down to the sort method on that
    via your own using hte containment method Jeff Louie suggested.
    "Gary" <gfehr@thoughtv ector.com> wrote in message
    news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=blue]
    > I would like to make a strongly typed, sortable collection by leveraging a
    > Framework class. I began by looking at CollectionBase but it doesn't have
    > built-int sorting. I would prefer to derive from ArrayList, but if I code
    > an Add(MyElement) method, the original Add(object) remains exposed,
    > compromising type safety. I know this must be a common task, how to do[/color]
    it?[color=blue]
    > Thanks,
    > Gary
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: type-safe collection

      Gary,[color=blue]
      > I would like to make a strongly typed, sortable collection by leveraging a
      > Framework class. I began by looking at CollectionBase but it doesn't have
      > built-int sorting.[/color]
      CollectionBase has "built-in" sorting by virtue its a wrapper around an
      ArrayList, the ArrayList itself is exposed via the protected
      CollectionBase. InnerList property.

      If your class needs to support Sort itself, I would recommend delegation to
      the InnerList sort methods.

      Something like (untested):

      using System.Collecti ons;

      class MyCollection : CollectionBase
      {

      ...

      void Sort()
      {
      base.InnerList. Sort()
      }
      void Sort(IComparer comparer)
      {
      base.InnerList. Sort(comparer)
      }
      void Sort(int index, int count, IComparer comparer)
      {
      base.InnerList. Sort(index, count, comparer)
      }
      }

      Hope this helps
      Jay

      "Gary" <gfehr@thoughtv ector.com> wrote in message
      news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=blue]
      > I would like to make a strongly typed, sortable collection by leveraging a
      > Framework class. I began by looking at CollectionBase but it doesn't have
      > built-int sorting. I would prefer to derive from ArrayList, but if I code
      > an Add(MyElement) method, the original Add(object) remains exposed,
      > compromising type safety. I know this must be a common task, how to do[/color]
      it?[color=blue]
      > Thanks,
      > Gary
      >
      >[/color]


      Comment

      • Ignacio Machin \( .NET/ C#  MVP \)

        #4
        Re: type-safe collection

        Hi Gary,

        Please find below an example of a strong typed collection, as you can see
        the correct way of doing so is extending CollectionBase.
        The only functionality not provided is Sort, for this I implemented a class
        : ClassSorter , it use reflection to sort any class based on a property
        without need anything from the sortee class.
        If you have any doubt let me know.

        Pd:
        The code is not well commented as I got it from the current working project
        that is not well documented yet.
        The Collection is a collection of a class type named Location, you need to
        change this to your correct type.
        If you look the Sort method of the collection class you will see that the
        first parameter is the name of the property that will be used as sort
        criteria.

        Hope this help,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        public class LocationCollect ion:CollectionB ase
        {
        public Location Insert( int index, Location newelem )
        {
        this.InnerList. Insert( index, newelem);
        return newelem;
        }
        public Location Add( Location newelem)
        {
        this.InnerList. Add( newelem);
        return newelem;
        }

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

        public Location Find(int id)
        {
        foreach(Locatio n current in InnerList)
        if ( current.ID == id )
        return current;
        return null;
        }

        public void Remove( Location elem)
        {
        InnerList.Remov e( elem);

        }


        public LocationCollect ion(){}
        private LocationCollect ion( ArrayList newarray)
        {
        InnerList.Clear ();
        foreach( Location location in newarray)
        {
        Add( location);
        }
        }


        public LocationCollect ion Sort( string sortParam,
        fatalcrashCore. SortDirection direction)
        {
        ArrayList newlist = (ArrayList)Inne rList.Clone();
        ClassSorter sorter = new ClassSorter( sortParam, SortByType.Prop erty,
        direction);
        newlist.Sort( sorter);
        return new LocationCollect ion( newlist);
        }

        public LocationCollect ion Clone()
        {
        return new LocationCollect ion( InnerList);
        }


        public LocationCollect ion Search(FilterCo llection filters, bool AND)
        {
        LocationCollect ion filtered = new LocationCollect ion ();
        foreach(Locatio n point in InnerList)
        {
        bool matched = false;
        foreach(FilterB ase filter in filters)
        if ( filter.Match(po int) )
        {
        matched = true;
        if ( !AND )
        break;
        }
        else
        {
        if ( AND )
        {
        matched = false;
        break;
        }
        }
        if ( matched )
        filtered.Add( point);

        }
        return filtered;
        }

        }

        public class ClassSorter: IComparer
        {
        protected string sortBy;
        protected SortByType sortByType;
        protected SortDirection sortDirection;


        #region Constructors
        public ClassSorter(str ing sortBy, SortByType sortByType, SortDirection
        sortDirection)
        {
        this.sortBy = sortBy;
        this.sortByType = sortByType;
        this.sortDirect ion = sortDirection;
        }
        #endregion

        int Compare( object x, object y, string comparer)
        {
        if ( comparer.IndexO f( ".") != -1 )
        {
        //split the string
        string[] parts = comparer.Split( new char[]{ '.'} );
        return Compare( x.GetType().Get Property( parts[0]).GetValue(x, null) ,
        y.GetType().Get Property( parts[0]).GetValue(y, null) , parts[1]
        );
        }
        else
        {
        IComparable icx, icy;
        icx =
        (IComparable)x. GetType().GetPr operty( comparer).GetVa lue(x, null);
        icy =
        (IComparable)y. GetType().GetPr operty( comparer).GetVa lue(y, null);

        if ( x.GetType().Get Property(compar er).PropertyTyp e ==
        typeof(System.S tring) )
        {
        icx = (IComparable) icx.ToString(). ToUpper();
        icy = (IComparable) icy.ToString(). ToUpper();
        }

        if(this.sortDir ection == SortDirection.D escending)
        return icy.CompareTo(i cx);
        else
        return icx.CompareTo(i cy);
        }

        }

        public int Compare(object x, object y)
        {
        return Compare( x, y, sortBy);
        }

        }

        public enum SortByType
        {
        Method = 0,
        Property = 1
        }

        public enum SortDirection
        {
        Ascending = 0,
        Descending = 1
        }





        "Gary" <gfehr@thoughtv ector.com> wrote in message
        news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=blue]
        > I would like to make a strongly typed, sortable collection by leveraging a
        > Framework class. I began by looking at CollectionBase but it doesn't have
        > built-int sorting. I would prefer to derive from ArrayList, but if I code
        > an Add(MyElement) method, the original Add(object) remains exposed,
        > compromising type safety. I know this must be a common task, how to do[/color]
        it?[color=blue]
        > Thanks,
        > Gary
        >
        >[/color]


        Comment

        • Gary

          #5
          Re: type-safe collection

          Thanks for all the great suggestions, folks, stuff for me to chew on!
          Gary

          "Gary" <gfehr@thoughtv ector.com> wrote in message
          news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=blue]
          > I would like to make a strongly typed, sortable collection by leveraging a
          > Framework class. I began by looking at CollectionBase but it doesn't have
          > built-int sorting. I would prefer to derive from ArrayList, but if I code
          > an Add(MyElement) method, the original Add(object) remains exposed,
          > compromising type safety. I know this must be a common task, how to do[/color]
          it?[color=blue]
          > Thanks,
          > Gary
          >
          >[/color]


          Comment

          • Gary

            #6
            Re: type-safe collection

            Thanks, I wasn't aware of this. This looks like the least effort route to
            the solution.
            Gary

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:etUIn1Y2DH A.2604@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Gary,[color=green]
            > > I would like to make a strongly typed, sortable collection by leveraging[/color][/color]
            a[color=blue][color=green]
            > > Framework class. I began by looking at CollectionBase but it doesn't[/color][/color]
            have[color=blue][color=green]
            > > built-int sorting.[/color]
            > CollectionBase has "built-in" sorting by virtue its a wrapper around an
            > ArrayList, the ArrayList itself is exposed via the protected
            > CollectionBase. InnerList property.
            >
            > If your class needs to support Sort itself, I would recommend delegation[/color]
            to[color=blue]
            > the InnerList sort methods.
            >
            > Something like (untested):
            >
            > using System.Collecti ons;
            >
            > class MyCollection : CollectionBase
            > {
            >
            > ...
            >
            > void Sort()
            > {
            > base.InnerList. Sort()
            > }
            > void Sort(IComparer comparer)
            > {
            > base.InnerList. Sort(comparer)
            > }
            > void Sort(int index, int count, IComparer comparer)
            > {
            > base.InnerList. Sort(index, count, comparer)
            > }
            > }
            >
            > Hope this helps
            > Jay
            >
            > "Gary" <gfehr@thoughtv ector.com> wrote in message
            > news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=green]
            > > I would like to make a strongly typed, sortable collection by leveraging[/color][/color]
            a[color=blue][color=green]
            > > Framework class. I began by looking at CollectionBase but it doesn't[/color][/color]
            have[color=blue][color=green]
            > > built-int sorting. I would prefer to derive from ArrayList, but if I[/color][/color]
            code[color=blue][color=green]
            > > an Add(MyElement) method, the original Add(object) remains exposed,
            > > compromising type safety. I know this must be a common task, how to do[/color]
            > it?[color=green]
            > > Thanks,
            > > Gary
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: type-safe collection

              Gary,
              FYI: the DictionaryBase is a wrapper around Hashtable, the Hashtable itself
              is exposed via the DictionaryBase. InnerHashtable.

              I actually wrote my own DictionaryBase & CollectionBase, that allow the
              developer to can change the wrapped collection. Which reminds me, I need to
              post them someplace convenient for others ;-)

              Hope this helps
              Jay

              "Gary" <gfehr@thoughtv ector.com> wrote in message
              news:40042b5e$0 $2652$9a6e19ea@ news.newshostin g.com...[color=blue]
              > Thanks, I wasn't aware of this. This looks like the least effort route to
              > the solution.
              > Gary
              >
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              > news:etUIn1Y2DH A.2604@TK2MSFTN GP09.phx.gbl...[color=green]
              > > Gary,[color=darkred]
              > > > I would like to make a strongly typed, sortable collection by[/color][/color][/color]
              leveraging[color=blue]
              > a[color=green][color=darkred]
              > > > Framework class. I began by looking at CollectionBase but it doesn't[/color][/color]
              > have[color=green][color=darkred]
              > > > built-int sorting.[/color]
              > > CollectionBase has "built-in" sorting by virtue its a wrapper around an
              > > ArrayList, the ArrayList itself is exposed via the protected
              > > CollectionBase. InnerList property.
              > >
              > > If your class needs to support Sort itself, I would recommend delegation[/color]
              > to[color=green]
              > > the InnerList sort methods.
              > >
              > > Something like (untested):
              > >
              > > using System.Collecti ons;
              > >
              > > class MyCollection : CollectionBase
              > > {
              > >
              > > ...
              > >
              > > void Sort()
              > > {
              > > base.InnerList. Sort()
              > > }
              > > void Sort(IComparer comparer)
              > > {
              > > base.InnerList. Sort(comparer)
              > > }
              > > void Sort(int index, int count, IComparer comparer)
              > > {
              > > base.InnerList. Sort(index, count, comparer)
              > > }
              > > }
              > >
              > > Hope this helps
              > > Jay
              > >
              > > "Gary" <gfehr@thoughtv ector.com> wrote in message
              > > news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=darkred]
              > > > I would like to make a strongly typed, sortable collection by[/color][/color][/color]
              leveraging[color=blue]
              > a[color=green][color=darkred]
              > > > Framework class. I began by looking at CollectionBase but it doesn't[/color][/color]
              > have[color=green][color=darkred]
              > > > built-int sorting. I would prefer to derive from ArrayList, but if I[/color][/color]
              > code[color=green][color=darkred]
              > > > an Add(MyElement) method, the original Add(object) remains exposed,
              > > > compromising type safety. I know this must be a common task, how to[/color][/color][/color]
              do[color=blue][color=green]
              > > it?[color=darkred]
              > > > Thanks,
              > > > Gary
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jacob

                #8
                Re: type-safe collection

                Strong-Typed collections based on CollectionBase, or an ArrayList???? Sure
                you could wrap it up and make it a strong-typed collection, but everything
                is still going to get cast as an object, and that will have a serious affect
                on performance. The easiest way to learn strong type collections or have
                one made for you is to use CollectionGen for CodeSmith:


                Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.


                I now write all my collections manually and I can always get better
                performance out of them than the standard .NET collection.

                Hope this helps,
                Jacob


                "Gary" <gfehr@thoughtv ector.com> wrote in message
                news:40035b72$0 $62138$9a6e19ea @news.newshosti ng.com...[color=blue]
                > I would like to make a strongly typed, sortable collection by leveraging a
                > Framework class. I began by looking at CollectionBase but it doesn't have
                > built-int sorting. I would prefer to derive from ArrayList, but if I code
                > an Add(MyElement) method, the original Add(object) remains exposed,
                > compromising type safety. I know this must be a common task, how to do[/color]
                it?[color=blue]
                > Thanks,
                > Gary
                >
                >[/color]


                Comment

                Working...