Can I sort a KeyedCollection ? If so how do I do it. Please, any help on this issue is very much appreciated. Thank you.
9 6905
Any idea's how I can sort a KeyedCollection . Thank you.
SammyB 807
Recognized Expert Contributor
Any idea's how I can sort a KeyedCollection . Thank you.
I cannot see anyway to do this. In fact, I cannot see any way to retrieve the keys from a collection. Can you convert your Collection to a SortedDictionar y? -
Dim s As New SortedDictionary(Of String, String)
-
s.Add("txt", "notepad.exe")
-
s.Add("bmp", "paint.exe")
-
s.Add("dib", "paint.exe")
-
s.Add("rtf", "wordpad.exe")
-
Notice that the key is first, not second as in collections.
Plater 7,872
Recognized Expert Expert
I could not find any object called KeyedCollection .
The only collections with Keys I have seen have a .Keys proprty that returns a string[]. And that is very easy to sort.
Must sort functions (like the generic Array.Sort() ) take a custom thing to sort objects (Icomparable or something like that) in which you can define you own method to decide which comes first between two objects.
SammyB 807
Recognized Expert Contributor
I could not find any object called KeyedCollection .
You've made my day: I almost posted the same reply. I think that bchirra is using an old Collection object: remember, they could have keys: -
Dim c As New Collection
-
c.Add("notepad.exe", "txt")
-
c.Add("paint.exe", "bmp")
-
c.Add("paint.exe", "dib")
-
c.Add("wordpad.exe", "rtf")
-
As you said, if you could get to the keys, sorting would be easy, but these keys seem to be write-only! I had no idea that the Collection object was still around. --Sam
Plater 7,872
Recognized Expert Expert
Ahh I see it's part of the VisualBasic compatibility. (And also in System.Collecti ons.ObjectModel .KeyedCollectio n)
You could also have a second single-dimensioned array (ArrayList is good)
wherein whenever your Collection object is populated, you add the value of the KEY to that second array.
Then all you need to do is sort that array. Remember: Only the Keys need to be sorted if you reference your Collection object using the value of the Key.
I would def say time to upgrade to an actual collection object. Since KeyedColelction is abstract you must be creating a custom class that inherits KeyedCollection , at which point YOU are in charge of coding in your custum method to retrieve the key. So you are really digging your own hole on that one.
Thank you for your replies.
Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collecti ons.ObjectModel (new class in .net 2.0).
Can I modify my class to inherit from generic SortedList? What is default sort order of the SortedList, Asce or Desc? Thank you.
Plater 7,872
Recognized Expert Expert
You could override the add function to track the key in your own way. (Still not sure why you are using this setup to begin with)
I did this, not sure it's correct: -
public class mylist : System.Collections.ObjectModel.KeyedCollection<string,int>
-
{
-
protected override string GetKeyForItem(int item)
-
{
-
int idx = base.IndexOf(item);
-
foreach (string n in base.Dictionary.Keys)
-
{
-
if (base[n] == item)
-
{
-
return n;//or something
-
}
-
}
-
throw new Exception("The method or operation is not finished.");
-
}
-
public ICollection<string> Keys
-
{
-
get
-
{
-
return base.Dictionary.Keys;
-
}
-
}
-
}
-
Thank you for your replies.
Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collecti ons.ObjectModel (new class in .net 2.0).
Can I modify my class to inherit from generic SortedList? What is default sort order of the SortedList, Asce or Desc? Thank you.
Thank you very much. I will try it out.
SammyB 807
Recognized Expert Contributor
Thank you for your replies.
Yes, I have a custom class that inherts from KeyedCollection which is part of
namespace: System.Collecti ons.ObjectModel (new class in .net 2.0).
Can I modify my class to inherit from generic SortedList? What is default sort order of the SortedList, Asce or Desc? Thank you.
If you have keys and you want to sort, then I would think that you would want to use SortedDictionar y as your base class.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Stuart |
last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the
degenerate case in a common situation). I have a replacement.
I would like to globally do "using namespace std; except use my
sort()".
I'd heard you can do this within a namespace:
namespace Fixed
{
using namespace std;
|
by: James T. |
last post by:
Hello!
How I can update an object in KeyedCollection. whileItem propery is Read
Only?
Thank you!
James
|
by: Craig Buchanan |
last post by:
is there an easy way to serialize on the keys in a KeyedCollection, rather
than the entire collection of objects? perhaps there is another option?
thanks for your time.
|
by: Brannon |
last post by:
So when you use VS's View Designer, each object included is keyed off
its Name. You cannot make the name the same as some other component.
You can change the name in a PropertyGrid. I want to do a similar
thing. I have a class (MissionManager with a bunch of static functions)
that contains a static KeyedCollection<Missionof my objects. Each
mission has a Name (and interface requring the name). That works fine.
What doesn't work is changing...
|
by: Dahab |
last post by:
Hi,
Anyone know if its possible to iterate through a hashtable in the same order
that the table was populated?
Im currently using an enumerator to iterate, but the order is quit different
from when the items where inserted into the hashtable.
Thanks
D.
| |
by: Wiebe Tijsma |
last post by:
Hi,
I'm trying to serialize a KeyedCollection with the SoapFormatter,
however I'm getting the exception:
System.Runtime.Serialization.SerializationException: Soap Serializer
does not support serializing Generic Types :
System.Collections.Generic.GenericEqualityComparer`1
I didn't expose any generic types however, so it's internal to the Keyed
|
by: Chris Mullins [MVP] |
last post by:
KeyedCollection is a very handy little class, that unforutnatly has a nasty
bug in it.
The bug (which I ran across) causes the following code to fail:
if (!rooms.Contains(room))
_rooms.Add(room);
The problem is that contains returns "false", but then Add throws an
exception because the item really is already in there.
|
by: GS |
last post by:
please bear with me, I already tried the built-in help, Google but not
finding the solution.
I want to use generic KeyedCollection
first crack
private class RegexHolder
{
String Name;
bool Tested;
RegexOptions regexOption;
|
by: Jon Slaughter |
last post by:
topic says it all? Is there any simple way? msdn says that the keyed
collection is build from both a sortedlist and sorted dictionary.
http://msdn2.microsoft.com/en-us/library/5z658b67.aspx
(well, it doesn't say that specifically but it says it when talking about
the sorted versions so I'm not sure)
I've found this
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |