Hi all, is there a key value collection in .Net that maintains the
ordering in which I add items.
I'm finding that I'm creating a number of classes that contain a name
and then some object. I would prefer just to use some collection that
maintains the ordering in which I add things (like ArrayList), but that
maps a key to a value.
I thought NameValueCollec tion was the right one - but it doesn't
guarantee ordering.
If it doesn't exist, why not? If it doesn't what is the "right" way to
build it? Just have wrap your class around an instance of ArrayList
and have some internal class that takes the key given in the Add method
and instantiates this generic class with two members (a string key and
an object value), then add that to the internal ArrayList?
Thanks,
Novice 23 11322
I would create a class that wraps an ArrayList and a Hashtable.
Whenever anything is added to your class, add it to both the ArrayList
and the Hashtable. When you want to return an iterator, return an
iterator into the ArrayList. When you want a lookup by key, look it up
in the Hashtable.
I was actually thinking of doing something like that originally since
it is all reference based, there would be minimal cost (memory) to do
something like that - i.e. to have the ArrayList and the Hashtable to
point to the various elements.
Does anyone know of any reason why I should just rip off the
implementation of PhoneNumberDict ionary (that extends DictionaryBase)
from here: http://www.codeproject.com/csharp/collection1.asp
I'm not doing a PhoneNumberDict ionary, but it seems to provide what I
want in terms of allowing me to specify an arbitrary name and an
arbitrary object. And when I get them all, they will be returned in
the order I added them.
Does that sound right?
Thanks,
Novice
Crud!
I've tried using DictionaryBase, but the compiler gives me
DictionaryBase could not be found. I guess that means it is a .Net 2
class?
That sucks.
Never mind - looks like the executable wasn't getting regenerated or
something - fresh build and the compiler was okay with it.
Well, this doesn't work, it doesn't maintain the order in which I added
them when I iterate through it.
So I guess I will just do something like (not compileable code):
class DictionaryEntry Collection
{
private ArrayList mKeyValues;
public DictionaryEntry Collection()
{
mKeyValues = new ArrayList();
}
public void Add (object key, object value)
{
DictionaryEntry keyValue = new DictionaryEntry (key, value);
mKeyValues.Add( keyValue);
}
public DictionaryEntry[] GetKeyValueElem ents()
{
//iterate over all the elements in my arraylist
}
}
Sound right?
Thanks,
Novice
Bruce Wood wrote: I would create a class that wraps an ArrayList and a Hashtable. Whenever anything is added to your class, add it to both the ArrayList and the Hashtable. When you want to return an iterator, return an iterator into the ArrayList. When you want a lookup by key, look it up in the Hashtable.
What if you add something with the same key twice? It will show up in
the hashtable just once but in the ArrayList twice. That is probably
not what he's looking for.
I'd create a new collection that wrapped an ArrayList<Objec t[]> and
massage the add method to check for duplicates, while maintaining order.
I just whipped something up in Java. Its untested, unsynchronized,
probably slow, and the syntax is slightly different for C# (the for
loop). Here it is; it might help you, it might not.
public class HashList {
private ArrayList<Objec t[]> coll;
public HashList() {
coll = new ArrayList<Objec t[]>();
}
public void add(Object key, Object value) {
boolean alreadyAdded = false;
for (Object[] s : coll) {
if (s[0] == key) {
int me = coll.indexOf(s) ;
coll.remove(me) ;
coll.add(me, s);
alreadyAdded = true;
}
}
if (!alreadyAdded) {
coll.add(new Object[] { key, value });
}
}
}
Novice,
You can just use the SortedList<TKey , TValue> generic class. It will
allow you to perform lookups, and order the list as well.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
<il***********@ gmail.com> wrote in message
news:11******** **************@ j73g2000cwa.goo glegroups.com.. . Hi all, is there a key value collection in .Net that maintains the ordering in which I add items.
I'm finding that I'm creating a number of classes that contain a name and then some object. I would prefer just to use some collection that maintains the ordering in which I add things (like ArrayList), but that maps a key to a value.
I thought NameValueCollec tion was the right one - but it doesn't guarantee ordering.
If it doesn't exist, why not? If it doesn't what is the "right" way to build it? Just have wrap your class around an instance of ArrayList and have some internal class that takes the key given in the Add method and instantiates this generic class with two members (a string key and an object value), then add that to the internal ArrayList?
Thanks, Novice
Nicholas,
new to Generics, is SortedList already generic?
Why can I ! say SortedList<stri ng,string> ??? Throws Error
if (!Page.IsPostBa ck)
{
AlwaysSortedLis t = new SortedList();
Session.Add("AS L",AlwaysSorted List);
}
in button handler....
((SortedList)Se ssion["ASL"]).Add(TextBox1. Text,TextBox1.T ext);
DropDownList1.D ataSource = ((SortedList)Se ssion["ASL"]); //AlwaysSortedLis t;
DropDownList1.D ataTextField = "key";
DropDownList1.D ataValueField = "value";
DropDownList1.D ataBind();
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** **********@TK2M SFTNGP04.phx.gb l... Novice,
You can just use the SortedList<TKey , TValue> generic class. It will allow you to perform lookups, and order the list as well.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard. caspershouse.co m
<il***********@ gmail.com> wrote in message news:11******** **************@ j73g2000cwa.goo glegroups.com.. . Hi all, is there a key value collection in .Net that maintains the ordering in which I add items.
I'm finding that I'm creating a number of classes that contain a name and then some object. I would prefer just to use some collection that maintains the ordering in which I add things (like ArrayList), but that maps a key to a value.
I thought NameValueCollec tion was the right one - but it doesn't guarantee ordering.
If it doesn't exist, why not? If it doesn't what is the "right" way to build it? Just have wrap your class around an instance of ArrayList and have some internal class that takes the key given in the Add method and instantiates this generic class with two members (a string key and an object value), then add that to the internal ArrayList?
Thanks, Novice
Yes. In VB 2005, you would use
dim AlwaysSortedLis t as new SortedList(of String, String)
AlwaysSortedLis t.add("Key1", "This is Key1")
debug.print(Alw aysSortedList(" Key1") displays "This is Key1" in the
Immediate window.
Mike Ober.
"MSDN" <sq**********@h otmail.com> wrote in message
news:eh******** ******@TK2MSFTN GP04.phx.gbl... Nicholas,
new to Generics, is SortedList already generic?
Why can I ! say SortedList<stri ng,string> ??? Throws Error
if (!Page.IsPostBa ck) { AlwaysSortedLis t = new SortedList(); Session.Add("AS L",AlwaysSorted List); }
in button handler....
((SortedList)Se ssion["ASL"]).Add(TextBox1. Text,TextBox1.T ext); DropDownList1.D ataSource = ((SortedList)Se ssion["ASL"]);
//AlwaysSortedLis t; DropDownList1.D ataTextField = "key"; DropDownList1.D ataValueField = "value"; DropDownList1.D ataBind(); "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:%2******** **********@TK2M SFTNGP04.phx.gb l... Novice,
You can just use the SortedList<TKey , TValue> generic class. It will allow you to perform lookups, and order the list as well.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard. caspershouse.co m
<il***********@ gmail.com> wrote in message news:11******** **************@ j73g2000cwa.goo glegroups.com.. . Hi all, is there a key value collection in .Net that maintains the ordering in which I add items.
I'm finding that I'm creating a number of classes that contain a name and then some object. I would prefer just to use some collection that maintains the ordering in which I add things (like ArrayList), but that maps a key to a value.
I thought NameValueCollec tion was the right one - but it doesn't guarantee ordering.
If it doesn't exist, why not? If it doesn't what is the "right" way to build it? Just have wrap your class around an instance of ArrayList and have some internal class that takes the key given in the Add method and instantiates this generic class with two members (a string key and an object value), then add that to the internal ArrayList?
Thanks, Novice
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: m. pollack. |
last post by:
Hi all,
Is there any information to be had about the "Object
Collection Editor" that appears when you click on a
collection property in the PropertyGrid control? I have a
class that maintains a collection of instances of the same
class, and I would like the user to be able to expand the
nested instances and view their properties.
For example, if I have a class called TestClass that has
|
by: keweiming |
last post by:
I have a project which needs to open hundreds to thousands of files for
writing. The following is a simplified test program I wrote to see if I
can use a map<string, ofstream> object to keep the list of ofstreams. I
need to have them open simultaneously for writing -- I have millions of
rows of data to write to them so opening and closing all the time will
be unacceptable in efficiency.
The following program compiles but failed to run....
|
by: Project2501a |
last post by:
hey guys,
Question about the internal workings of Access. How are controls added
in the Form.Controls collection? I mean, in which order?
the order place them on the form? is there a way to re-arrange them as
i please? My solution is to remove the objects from the Form.Controls
collection, place them in a tmpCollection, arrange tmpCollection as I
please, and then re-add them back into the Form.Controls collection.
thanks
|
by: Frank |
last post by:
Our system maintains session state using the ASP.NET State Server service. We expect some of our session state objects to be over 85K, which categorizes them to be VLO's (very large objects) in terms of .NET memory allocations. This means that these objects will be allocated in a special managed heap for large objects. This makes sense since this would require some work for the garbage collector to move and compact these objects during a GC...
|
by: marcuslm |
last post by:
I need help figuring out which collection to use. Maybe I need to create my
own but here's what I need.
A collection that...
1. provides ability to access values via a key
2. maintains the order in which I add it to the collection (ie when I
iterate through it, they come out the same order in which I put them in)
Help please. Thanks!!!
| |
by: Michael D. Ober |
last post by:
In the code below, the IComparator function is never called. What am I
missing?
Public Class ArchiveInfo
Implements System.IComparable(Of ArchiveInfo)
Public FullName As String = ""
Public AccountNumber As String = ""
Public Sub New(ByRef Account As String, ByVal fname As String)
|
by: Martin R |
last post by:
Hi,
How to find first not null value in column whitout chacking whole table
(if there is a not null value then show me it and stop searching, the
table is quite big)?
thx,
Martin
*** Sent via Developersdex http://www.developersdex.com ***
|
by: evanburen |
last post by:
When my page loads, I check for the existence of a cookie value through
readCookie(). If there is a value present for the cookie, I would like
that to be the default value in function ordering() and that value to
be the SELECTED value in ddlProfileNames. In other words, if there is
no cookie value, then function ordering will default to
ordering("Div1,Div2,Div3,Div4"). If there is a value, then that should
be used in function ordering...
|
by: zephyr |
last post by:
Hi,
I have a hidden field in my form:
<input type="hidden" name="checkBoxesCollection" value="">
If I submit the form I call a javascript function:
function storeCheckboxFieldNames(theForm){
var inputElements = theForm.getElementsByTagName("input");
var collection = document.getElementsByName("checkBoxesCollection");
alert(collection.value);
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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: 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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |