473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

IDictionary

CD
Hi,
I'm trying to learn C#. I'm coming from Smalltalk. I need a good example of
implementing a Dictionary (Keys->values) without having to create my own
subclass.
I see how to add, but not sure how to create and instance, how to access the
value for one key

thanks
Jul 21 '05 #1
2 1380
Hi CD,

Hmm...

If by "a good example of implementing a Dictionary" you mean *using* an
existing dictionary class, then the Hashtable class implements the
IDictionary interface, and can be used as a dictionary.

Create a C# console application, and paste this code into/over the Main
method call:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Hashtable h = new Hashtable();
h.Add(@"001", @"One");
h.Add(@"002", @"Two");
h.Add(@"003", @"Three");
h.Add(@"004", @"Four");

// Access values via keys
Console.WriteLine(@"ONE = {0}", h[@"001"]);
Console.WriteLine(@"THREE = {0}", h[@"003"]);
Console.ReadLine();
}

Hope this helps...

regards
Patrick
"CD" wrote:
Hi,
I'm trying to learn C#. I'm coming from Smalltalk. I need a good example of
implementing a Dictionary (Keys->values) without having to create my own
subclass.
I see how to add, but not sure how to create and instance, how to access the
value for one key

thanks

Jul 21 '05 #2
CD
Yes it does.
Thanks

"P.Simpe-Asante" <PS**********@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
Hi CD,

Hmm...

If by "a good example of implementing a Dictionary" you mean *using* an
existing dictionary class, then the Hashtable class implements the
IDictionary interface, and can be used as a dictionary.

Create a C# console application, and paste this code into/over the Main
method call:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Hashtable h = new Hashtable();
h.Add(@"001", @"One");
h.Add(@"002", @"Two");
h.Add(@"003", @"Three");
h.Add(@"004", @"Four");

// Access values via keys
Console.WriteLine(@"ONE = {0}", h[@"001"]);
Console.WriteLine(@"THREE = {0}", h[@"003"]);
Console.ReadLine();
}

Hope this helps...

regards
Patrick
"CD" wrote:
Hi,
I'm trying to learn C#. I'm coming from Smalltalk. I need a good example
of
implementing a Dictionary (Keys->values) without having to create my own
subclass.
I see how to add, but not sure how to create and instance, how to access
the
value for one key

thanks

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: greg | last post by:
Hi I use a COM object to interface with domain search from SRS and it returns: SRSPLUSLib.SrsClass srs = new SRSPLUSLib.SrsClass(); srs.Startup(); SRSPLUSLib.IDictionary dic =...
2
by: tonci.tomic | last post by:
I have IDictionary with integers as keys and values. Suppose I want to set value for key=42 to 1 if entry doesn't exist in container. Otherwise, I want to increment value. I ended with: ...
4
by: Sanjay Vyas | last post by:
Sorry, forgot to cross post this one.. This is rather unusual as we would expect any Collection class to implement ICollection interface and furthermore a Dictionary class should implement...
4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
1
by: John | last post by:
I have a collection of properties from a WMI query and I want to recreate the collection and exclude duplicates and then enumerate. In VBScript I used the "Scripting.Dictionary" object and the...
3
by: Marek | last post by:
Hi I have a class which has a property which implements IDictionary which I would like to display in a propertygrid control. If I add the property expands, but I only get Count, IsReadOnly,...
10
by: J055 | last post by:
Hi I have an OrderedDictionary object where the key is an enum. Is there an easy way to cast it to an integer? Examples/document appreciated. Thanks Andrew
7
by: vcquestions | last post by:
trying to design an effiecient interface, where I can pass an IDictionary<int,string^to a method. int is some id and string^ is some value that is initialized to null by a caller. The callee...
1
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
I have repeater and IDictionary. In dictionary I add values like name and age. IDictionary TestDictionary = new Hashtable(); SiteImages.Add(23, "John Smith"); SiteImages.Add(14, "Some Some1"); ...
3
by: coconet | last post by:
I have a string like this mystring = "color1:blue;color2:red"; I am tring to convert this into an IDictionary populated like this: color1 blue color2 red My non-working syntax is
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.