473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Nov 15 '05 #1
7 2766
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" <gf***@thoughtvector.com> wrote in message
news:40***********************@news.newshosting.co m...
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

Nov 15 '05 #2
Gary,
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. 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.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:40***********************@news.newshosting.co m... 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

Nov 15 '05 #3
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 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:40***********************@news.newshosting.co m...
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

Nov 15 '05 #4
Thanks for all the great suggestions, folks, stuff for me to chew on!
Gary

"Gary" <gf***@thoughtvector.com> wrote in message
news:40***********************@news.newshosting.co m...
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

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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
Gary,
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. 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.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:40***********************@news.newshosting.co m...
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


Nov 15 '05 #6
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" <gf***@thoughtvector.com> wrote in message
news:40**********************@news.newshosting.com ...
Thanks, I wasn't aware of this. This looks like the least effort route to
the solution.
Gary

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...
Gary,
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. 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.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:40***********************@news.newshosting.co m...
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



Nov 15 '05 #7
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:

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:40***********************@news.newshosting.co m...
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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
3
by: amanjsingh | last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.