Solved

c# strange collection issue

Posted on 2011-09-27
3
196 Views
Last Modified: 2013-12-17
My old requirement was to maintain a list of 5 recently clicked product ids so that the last clicked was first in the list.

I did this by storing a semicolon delimited string, splitting it into an array, removing the number to be added from the array (if it existed), then adding it to the front of the list.  Creating a new array from a split function, then saving the proper number of elements.  This would ensure that if my new product was already clicked, it would move to the front.
so
5;3;6;1;7 and clicking on 7 would yelid 7;3;6;1

I just found out that I need to do the same thing, but maintain the list of 5 products per a list of 5 companines.
So
company1=5;3;6;1;7
company2=10
company3=21;22;28
clicking on company2 and product 13 would give me company2=10

If I already have 5 companines and I click on a new one, add it to the top, and get rid of the last.  

If the company already exists, add it to the beginning of the list, and add the product to the beginning of that list.
I have no idea how to tackle this.

Thanks
0
Comment
Question by:jackjohnson44
3 Comments
 
LVL 4

Accepted Solution

by:
theHollow earned 500 total points
ID: 36712941
Hi
If I understand correctly, you can use a Dictionary.

Example:
Dictionary<String, List<Int32>> companydict;

that way, you can have the "top 5" list per key in the dictionary.
The key will be (in my example) a the company name. But you can change it to a custom made company class.

companydict = new Dictionary<String, List<Int32>>();
companydict.Add("Company #1", new List<Int32>() { 1, 6, 3, 7, 4 });
companydict.Add("Company #2", new List<Int32>() { 7, 34, 14, 43 });
companydict.Add("Company #3", new List<Int32>() { 77, 1 });

Open in new window

(the numbers are all examples).

You can then access the "top 5" list within each company name by doing like this:
companydict["Company #2"] will get you the list for "Company #2", which you can work with.

Example:
List<Int32> lst = companydict["Company #2"];
lst.Add(10);

...or directly:
companydict["Company #2"].Add(10);

But there are probably lots of good alternative ways of doing this.
I hope I have understood the problem correctly :-)
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 36713739
OK, I believe the answers are given in other posts.

"...you can use a Dictionary..."
Forgive me but this is an insane idea.
Dictionaries are very good collections but not in this case. You cannot guarantee an order in a Dictionary! Moreover, Dictionary has NO any idea about the order elements were added! It is a good idea to understand the underlying mechanism for hash-based collections (maps, dictionaries).
0
 

Author Comment

by:jackjohnson44
ID: 36718901
That is correct, a dictionary won't work because I need to maintain the order.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
visual Basic Net vs c# net 8 54
Windows Service with UDP 2 28
Please explain: Aspect Oriented Programming 2 53
Code enhancement 5 12
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now