|
-
March 20th, 2005, 11:24 AM
#1
IDictionary example
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.
-
March 20th, 2005, 01:37 PM
#2
Re: IDictionary example
 Originally Posted by nyc_newbie
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
Code:
[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.
-
March 20th, 2005, 01:47 PM
#3
Re: IDictionary example
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
-
March 20th, 2005, 02:51 PM
#4
Re: IDictionary example
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.
-
March 20th, 2005, 03:08 PM
#5
Re: IDictionary example
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
-
March 20th, 2005, 03:36 PM
#6
Re: IDictionary example
when you want to add an entery to a hashtable you should give two things
this is the definition of Add function for a HashTable,
[C#]
public virtual void Add(
objectkey,
objectvalue
);
as you see value and key are objects so they can be everything like int,string,Myclass,
Code:
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.
Last edited by mehdi62b; March 20th, 2005 at 03:40 PM.
-
March 20th, 2005, 04:35 PM
#7
Re: IDictionary example
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|