UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> C# Custom Collection    
 
   
jleach
post Nov 29 2011, 07:02 AM
Post #1

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



Hi,

I'm looking to create a custom collection in C# that provides an ordered list of objects (all the same type). I'm working through this MSDN article: Walkthrough, Creating your own Collection Classes, which seems straightfoward enough, but I'm not sure if there's a better way to implement some sort of "AddAtPosition" method.

I'm thinking to sort my collection objects by an index, but would like the ability to add at any position, and to reorder the objects as well.

The ideal is to provide the user with a listbox of items that they can re-order/add/remove from.

Can anyone give some pointers as to the best way to build the collection structure?

Thanks,
Go to the top of the page
 
+
datAdrenaline
post Nov 29 2011, 07:13 AM
Post #2

UtterAccess Editor
Posts: 15,965
From: Northern Virginia, USA



I use ...

List<someObject> myCollection;

Then add my items to the collection. Then I use LINQ to order them however I need to.
Go to the top of the page
 
+
jleach
post Nov 29 2011, 07:52 AM
Post #3

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



I'll look into it. Thanks,
Go to the top of the page
 
+
jleach
post Nov 29 2011, 08:25 AM
Post #4

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



QUOTE (datAdrenaline @ Nov 29 2011, 07:13 AM) *
I use ...

List<someObject> myCollection;

Then add my items to the collection. Then I use LINQ to order them however I need to.


I like it. Thanks Brent. So many options... finding the most appropriate is... (IMG:style_emoticons/default/dazed.gif)
Go to the top of the page
 
+
datAdrenaline
post Nov 29 2011, 09:04 AM
Post #5

UtterAccess Editor
Posts: 15,965
From: Northern Virginia, USA



I am sorry my reply was a little sparse! I was running late to work. But ... I wanted to indicate that I personally view the Dictionary techniques and such that you linked to in your original post as "older" -- definately still valid, usable and all that jazz -- and LINQ (Language INtegrated Query) with List generics to be more "modern" (especially LINQ). If you want, I know I can dig up samples of using LINQ with DataTables as well as LINQ with List collections. Also, you may want to research LINQToSQL if you are working with SQL Server backends and LINQToXML if you are working with XML data sources.

There are indeed soooo many ways to do things -- I have definately had my mind in pretzles as I continue upon the learning path with C# (.Net in general). With respect to collections, I find that using List generics and LINQ is so blasted powerful I have a hard time thinking about using anything else! Although you can use LINQ against Arrays too.
Go to the top of the page
 
+
jleach
post Nov 29 2011, 10:03 AM
Post #6

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



I've peeked at LINQ before but hadn't yet used it really... now I'm getting deeper into reading up and testing scenarios using it, and I gotta say... this LINQ stuff is simply awesome!

I've already tested some LINKToXML and have breezed over LINKToSQL as well.

Some day I might take you up on the LINQ/DataTables offer, though for this particular app I'm working with a few local XML files and no SQLServer/Access/SQLite stuff.

Thanks for the insights though, much appreciated. A steep curve, but a fun climb (IMG:style_emoticons/default/dance.gif)
Go to the top of the page
 
+
rabroersma
post Nov 29 2011, 10:03 AM
Post #7

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



QUOTE (jleach @ Nov 29 2011, 04:02 AM) *
I'm looking to create a custom collection in C# that provides an ordered list of objects (all the same type).
Are you trying to (1) create a custom collection classes that provides a different kind of sorting, or are you trying to use stock collection class that uses type safe generics?

(1) - I expected there are already collections that do want you want. Take a look at SortedList() it looks like it will do what you want.
Go to the top of the page
 
+
jleach
post Nov 29 2011, 10:21 AM
Post #8

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



Hi Richard,

I'm looking for sort/add-in-place ability rather than specific type safety. What I'm working with is a list of folder paths, but I want the user to be able to sort them according to how often they're used, rather than by name or path. I thought to store each entry with an Index value to denote it's position:

0: \\ThisPath (default)
1: \\ThatPath
2: \\OtherPath

I was hoping to be able to add an entry at a given spot: say, after index 1, which would place the new path at index 2 and kick the existing 2 up to 3, etc etc.

0: \\ThisPath
1: \\ThatPath
2: \\NewPath
3: \\OtherPath

I wasn't sure if there was an existing collection to do that - I didn't find anything on it (although RemoveAt is available), so I thought to write my own - it looked like implementing CollectionBase could have been a start, then writing the custom logic for the Add.

Per Brent's suggestion of using LINQ, I think I see a few avenues available there as well.

Thanks,
Go to the top of the page
 
+
rabroersma
post Nov 29 2011, 10:43 AM
Post #9

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



The linkedlist allows insertion at any point. I'm not familiar with the .Net API, but I expect that there are utility classes that will sort a LinkedList if provided with a comparator. You could sort first by the usage property, then allow the user to alter the sorted list where desired.
Go to the top of the page
 
+
jleach
post Nov 29 2011, 10:45 AM
Post #10

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



I'll give that a look as well. Thanks,
Go to the top of the page
 
+
datAdrenaline
post Nov 29 2011, 01:07 PM
Post #11

UtterAccess Editor
Posts: 15,965
From: Northern Virginia, USA



I guess I would not worry about insertion point. I would just store the data needed to sort on, then sort the collection with LINQ. Something like this:

CODE
        struct MyItem
        {
            public int Frequency;
            public string Path;
        };

        public void mysortablelist()
        {
            List<MyItem> myCollection = new List<MyItem>(); //in System.Collections.Generic
            
            myCollection.Add(new MyItem { Frequency = 0, Path = "My Path 1" });
            myCollection.Add(new MyItem { Frequency = 1, Path = "My Path 2" });
            myCollection.Add(new MyItem { Frequency = 2, Path = "My Path 3" });
            myCollection.Add(new MyItem { Frequency = 3, Path = "My Path 4" });

            myCollection = (from i in myCollection
                            orderby i.Frequency descending
                            select i).ToList();  //using System.Linq;
            
            //more code here to use your sorted list as you see fit.
        }
Go to the top of the page
 
+
jleach
post Nov 29 2011, 09:53 PM
Post #12

UtterAccess Editor
Posts: 6,711
From: Capital District, NY, USA



I did manage a working implementation here - maybe not the best, but it works, so for the time being it'll do.

I ended up using a class for a Path, another static class for Paths, which maintains a private _items lists, along with logic for adding/removing, etc (not implemented from IEnumerable or ICollection though, babysteps...). In the static Paths class is a SortedItems property that uses Linq to return the appropriate sorted List.

I've got a slight mix of objects and Linq, pulled from an XML source, stored in memory for runtime with a single collection class to work off. It's not 100% what I set out for, but pretty darn close - 95% maybe. It'll certainly do.

Many thanks for the input.

Cheers,
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 20th May 2013 - 01:02 AM