Click to See Complete Forum and Search --> : IDictionary example


nyc_newbie
March 20th, 2005, 10:24 AM
Hello everyone,

in STIL I would just write a declaration something like:

std::map<string, T> _map;

and viola have _map as a map-like structure. My question is how to perform the equivalent in C# w/ IDictionary. I take it that I have to make a concrete implementation of the interface class. Is this right? And if so, why? And, does anyone have a simple soup-to-nuts example... I'm really interested in getting on w/ my code rather than writing implementations, but I will if necessary.

Thank you,
nyc_newbie.

mehdi62b
March 20th, 2005, 12:37 PM
I take it that I have to make a concrete implementation of the interface class. Is this right
you don't need to implement IDictinary just make a HashTable(or SortedList==Sortedhastable)
you can give its key and entry every object(it accepts object)
HasTable implemements IDictinary and you can consider it as a IDictinary

[C#]
using System;
using System.Collections;
public class SamplesHashtable {

public static void Main() {

// Create and initialize a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("First", "Hello");
myHT.Add("Second", "World");
myHT.Add("Third", "!");

// Display the properties and values of the Hashtable.
Console.WriteLine( "myHT" );
Console.WriteLine( " Count: {0}", myHT.Count );
Console.WriteLine( " Keys and Values:" );
PrintKeysAndValues( myHT );
}


public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
Console.WriteLine();
}
}
/*
Output:

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


HtH,

mehdi,
software student.

nyc_newbie
March 20th, 2005, 12:47 PM
Thanks for your reply. More questions: when do I need to implement my own version of IDictionary, and what, in a nutshell, is the difference between a HashTable and a Dictionary?

-Newbie

mehdi62b
March 20th, 2005, 01:51 PM
I don't know when, it depends on you requierment,

as you know HashTable implements some interfaces(abstract) one of them is IDictinary so we can say HashTable is derived from IDictinary so every HashTable is a IDictinary

public class Hashtable : IDictionary, ICollection, IEnumerable,
ISerializable, IDeserializationCallback, ICloneable

so everywhere in .NET framework in a function you need an IDictinary you can give a HashTable(because every hashtable is a IDictionary)

mehdi,
software student.

nyc_newbie
March 20th, 2005, 02:08 PM
Another question: aren't the types of the keys and, seperately, the value fixed? I'm looking for something like Dict<K, V> dict = new Dict<K, V>(); That's the syntax and methodology I was expecting. I've played around w/ your code and find that I can put in any types for keys and values and I can mix types.

So how is Dict built: In general any types can be used for any key and value? I have to use constraints to ensure the applied types are constructable and comparible, is this the idea?

Thanks,
-Newbie

mehdi62b
March 20th, 2005, 02:36 PM
when you want to add an entery to a hashtable you should give two things

key
its value
this is the definition of Add function for a HashTable,

[C#]
public virtual void (http://mk:@msitstore:E:Visual%20StudioMSDN2001OCT1033cpref.chm::/html/frlrfsystemvoidclasstopic.htm) Add(
object (http://mk:@msitstore:E:Visual%20StudioMSDN2001OCT1033cpref.chm::/html/frlrfsystemobjectclasstopic.htm)key,
object (http://mk:@msitstore:E:Visual%20StudioMSDN2001OCT1033cpref.chm::/html/frlrfsystemobjectclasstopic.htm)value
);

as you see value and key are objects so they can be everything like int,string,Myclass,

Hashtable ht=new Hashtable();
ht.Add("string","tehran");
ht.Add(1,12);

just keys need GetHashCode function which is implemented in object so in other objects it inherits(or you can override it)

I don't get your meaning about constructable and comparible(I don't know C++)

mehdi,
software student.

nyc_newbie
March 20th, 2005, 03:35 PM
Hi, well I now have most of what I was looking for. Its much better to have something running and then incrementally improve. I really appreciate your help. Thank you.