Make sure you check your boundary conditions on that code <g>...
Another approach that I often take for mover type lists is to just swap
items. This is a little more efficient and avoids the whole index
re-calculation bit and remove and add process. As items are moved you just
store one of the items into a temporary variable, then move the other into
the old slot, then write the saved var to the original slot.
+++ Rick ---
Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog
http://www.west-wind.com/wwthreads
----------------------------------
Making waves on the Web
> -----Original Message-----
> From: padnug@xxxxxxxxxxxxxxx [mailto:padnug@xxxxxxxxxxxxxxx]
> On Behalf Of Keith R. Pinster
> Sent: Tuesday, August 23, 2005 12:55 PM
> To: padnug@xxxxxxxxxxxxxxx
> Subject: [padnug] SOLUTION: Moving object order in a collection in C#
>
> Here's how I decided to do it:
>
> Instead of using an Order field, I use the following code in
> the MoveUp and MoveDown methods:
>
> public void MoveDown(CarPref carPref)
> {
> CarPref carCopy = new CarPref(carPref.Model, carPref.Make);
> List.Insert(List.IndexOf(carPref)+2, carCopy );
> List.Remove(carPref);
> }
> public void MoveUp(CarPref carPref)
> {
> CarPref carCopy = new CarPref(carPref.Model, carPref.Make);
> List.Insert(List.IndexOf(carPref)-1, carCopy );
> List.Remove(carPref);
> }
>
> I haven't tested it yet, but it looks like it'll work. I'll
> report back once I have it running, just in case anyone else
> is interested.
>
> --- In padnug@xxxxxxxxxxxxxxx, "Keith R. Pinster" <dimondwoof@xxxx>
> wrote:
> > I've got an object that has a set of sub-objects. I want to be
> able to
> > retrieve the sub-objects in a certain order. Is there a way to
> change
> > the index of the sub-objects?
> >
> > E.G:
> > Favorites:
> > Cars:
> > 1. Corvette
> > 2. Nova
> > 3. Firebird
> >
> > User wants to change the order so that it reads:
> > 1. Corvette
> > 2. Firebird
> > 3. Nova
> >
> > Currently I'm tracking the order with an Order int field, but this
> > seems really messy and error prone. Any ideas?
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~--> <font face=arial size=-1><a
> href="http://us.ard.yahoo.com/SIG=12ht3v4hd/M=362335.6886445.7
> 839731.1510227/D=groups/S=1705007709:TM/Y=YAHOO/EXP=1124834142
> /A=2894361/R=0/SIG=13jmebhbo/*http://www.networkforgood.org/to
> pics/education/digitaldivide/?source=YAHOO&cmpgn=GRP&RTP=http:
> //groups.yahoo.com/">In low income neighborhoods, 84% do not
> own computers. At Network for Good, help bridge the Digital
> Divide!</a>.</font>
> --------------------------------------------------------------
> ------~->
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
------------------------ Yahoo! Groups Sponsor --------------------~-->
<font face=arial size=-1><a
href="http://us.ard.yahoo.com/SIG=12hh8l43t/M=362329.6886308.7839368.1510227/D=groups/S=1705007709:TM/Y=YAHOO/EXP=1124836084/A=2894321/R=0/SIG=11dvsfulr/*http://youthnoise.com/page.php?page_id=1992
">Fair play? Video games influencing politics. Click and talk back!</a>.</font>
--------------------------------------------------------------------~->
|