HashTables are a bunch of nonordered objects reordering themselves to chaos

Ok as a VB programmer I was used to collections instead of Hashtables....Today I used a hashtable cause i thought a hashtable was almost the same as a collection.

I also thought the HashTable was the C# equivalent of a collection, So I started a nice VB project....

ms.Add("key3", Nothing)
ms.Add("key2",
Nothing
)
ms.Add("key1",
Nothing
)
For Each key As String In
ms.Keys
       Debug.WriteLine(key)
Next

This produced :

key1
key3
key2

So the order in the hashtable NOT the order they were added to the hashtable....
I think I stick to the collection.....

Dim ms As New Collection
ms.Add("key3", "key3")
ms.Add("key2", "key2")
ms.Add("key1", "key1")
For x As Integer = 1 To
ms.Count
       Debug.WriteLine(ms(x))
Next

Produces

key3
key2
key1

btw what is the real equivalent of a collection in C#.....come on give a reply and help me on my way.....

Published Fri, May 13 2005 4:06 PM by Patrick Wellink
Filed under:

Comments

# Using VB collections in C#

Friday, May 13, 2005 3:15 PM by TrackBack

# re: HashTables are a bunch of nonordered objects reordering themselves to chaos

Friday, May 13, 2005 7:10 PM by Patrick Wellink


That will be;

ms.Add("key3", null);
ms.Add("key2", null);
ms.Add("key1", null);
foreach (string key in ms.Keys) {
Debug.WriteLine(key);
}

and

Collection ms = new Collection();
ms.Add("key3", "key3");
ms.Add("key2", "key2");
ms.Add("key1", "key1");
for (int x = 1; (x <= ms.Count); x++) {
Debug.WriteLine(ms(x));
}

# re: HashTables are a bunch of nonordered objects reordering themselves to chaos

Friday, May 13, 2005 8:16 PM by Patrick Wellink
Patrick, why not try the SortedList object. It sorts the list on the key you specify:

using System;
using System.Collections;
public class SamplesSortedList {

public static void Main() {

// Creates and initializes a new SortedList.
SortedList mySL = new SortedList();
mySL.Add("First", "Hello");
mySL.Add("Second", "World");
mySL.Add("Third", "!");

// Displays the properties and values of the SortedList.
Console.WriteLine( "mySL" );
Console.WriteLine( " Count: {0}", mySL.Count );
Console.WriteLine( " Capacity: {0}", mySL.Capacity );
Console.WriteLine( " Keys and Values:" );
PrintKeysAndValues( mySL );
}


public static void PrintKeysAndValues( SortedList myList ) {
Console.WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList.Count; i++ ) {
Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
}
Console.WriteLine();
}
}
/*
This code produces the following output.

mySL
Count: 3
Capacity: 16
Keys and Values:
-KEY- -VALUE-
First: Hello
Second: World
Third: !
*/

# re: HashTables are a bunch of nonordered objects reordering themselves to chaos

Friday, May 13, 2005 11:09 PM by Patrick Wellink
i guess kris you are missing the point....

are you saying the order in wich you insert is preserved ????

# re: HashTables are a bunch of nonordered objects reordering themselves to chaos

Saturday, May 14, 2005 8:54 AM by Patrick Wellink
Check out this link: http://bloggingabout.net/jschreuder/archive/2005/05/13/3963.aspx

You CAN use collections in C#.

# re: HashTables are a bunch of nonordered objects reordering themselves to chaos

Saturday, May 14, 2005 11:28 AM by Patrick Wellink
ja jan die haddik natuurlijk al meteen gelezen.......

je ken ook gewoon meteen in VB. beginnen....

Dun heb je ook meteen een normal Isnummeric ;-)

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 1 and 6 and type the answer here: