http://www.geocities.com/jeff_louie/OOP/oop7.htm
Regards,
Jeff
>I would prefer to derive from ArrayList, but if I code
an Add(MyElement) method, the original Add(object) remains exposed,
compromising type safety.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Override Add(Object) and check the type of the input object, and throw an
exception if it is the wrong type. Not a perfect solution, since it is a
run-time error, but strongly-typed collections won't be right until
generics, anyway.
David
If your class needs to support Sort itself, I would recommend delegation to
the InnerList sort methods.
Something like (untested):
using System.Collections;
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" <gf...@thoughtvector.com> wrote in message
news:40035b72$0$62138$9a6e...@news.newshosting.com...
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 LocationCollection:CollectionBase
{
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(Location current in InnerList)
if ( current.ID == id )
return current;
return null;
}
public void Remove( Location elem)
{
InnerList.Remove( elem);
}
public LocationCollection(){}
private LocationCollection( ArrayList newarray)
{
InnerList.Clear();
foreach( Location location in newarray)
{
Add( location);
}
}
public LocationCollection Sort( string sortParam,
fatalcrashCore.SortDirection direction)
{
ArrayList newlist = (ArrayList)InnerList.Clone();
ClassSorter sorter = new ClassSorter( sortParam, SortByType.Property,
direction);
newlist.Sort( sorter);
return new LocationCollection( newlist);
}
public LocationCollection Clone()
{
return new LocationCollection( InnerList);
}
public LocationCollection Search(FilterCollection filters, bool AND)
{
LocationCollection filtered = new LocationCollection ();
foreach(Location point in InnerList)
{
bool matched = false;
foreach(FilterBase filter in filters)
if ( filter.Match(point) )
{
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(string sortBy, SortByType sortByType, SortDirection
sortDirection)
{
this.sortBy = sortBy;
this.sortByType = sortByType;
this.sortDirection = sortDirection;
}
#endregion
int Compare( object x, object y, string comparer)
{
if ( comparer.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);
icy =
(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);
if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )
{
icx = (IComparable) icx.ToString().ToUpper();
icy = (IComparable) icy.ToString().ToUpper();
}
if(this.sortDirection == SortDirection.Descending)
return icy.CompareTo(icx);
else
return icx.CompareTo(icy);
}
}
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" <gf...@thoughtvector.com> wrote in message
news:40035b72$0$62138$9a6e...@news.newshosting.com...
"Gary" <gf...@thoughtvector.com> wrote in message
news:40035b72$0$62138$9a6e...@news.newshosting.com...
"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@msn.com> wrote in message
news:etUIn1Y2...@TK2MSFTNGP09.phx.gbl...
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" <gf...@thoughtvector.com> wrote in message
news:40042b5e$0$2652$9a6e...@news.newshosting.com...
http://www.sellsbrothers.com/tools/
http://www.kynosarges.de/Templates.html
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" <gf...@thoughtvector.com> wrote in message
news:40035b72$0$62138$9a6e...@news.newshosting.com...