Getting a value out of StringDictionary

J

John

Hi! I have a StringDictionary itialized as follows:

private void frmICUCalculations_Activated(object sender, System.EventArgs e)
{
StringDictionary dataDict = new StringDictionary();
dataDict.Add("Item1", "2001" );
dataDict.Add( "Item2", "2002" );
dataDict.Add( "Item3", "2003" );
}

Now, I want to 'extract' the Value associated with the Key, as follows:

string myValue = dataDict.Item( "Item3" );

This doesn't work .... how should I do this? [The online example
iterates through the entire StringDictionary using the Iiterator; surely
there's a better way....)

Thanks for any and all help!

John F.
 
W

William Ryan

Use a Hashtable ;-). However, you can use the ContainsKey Method...or the
ContainsValue...

Also, have you tried using the Item method.

string myValue = dataDict.Item("Item3") //that should do it for you..hth

Bill
 
J

Jon Skeet [C# MVP]

John said:
Hi! I have a StringDictionary itialized as follows:

private void frmICUCalculations_Activated(object sender, System.EventArgs e)
{
StringDictionary dataDict = new StringDictionary();
dataDict.Add("Item1", "2001" );
dataDict.Add( "Item2", "2002" );
dataDict.Add( "Item3", "2003" );
}

Now, I want to 'extract' the Value associated with the Key, as follows:

string myValue = dataDict.Item( "Item3" );

This doesn't work .... how should I do this? [The online example
iterates through the entire StringDictionary using the Iiterator; surely
there's a better way....)

Use:

string myValue = dataDict["Item3"];
 
J

John

Well, it's odd but when I do this I get the compiler error:
'System.Collections.Specialized.StringDictionary" does not contain a
definition for 'Item'

Why this should be is not clear, since clearly 'Item' is a member of
that class....

Help!

Thanks in advance for any elucidation....

John F.

John said:
Hi! I have a StringDictionary itialized as follows:

private void frmICUCalculations_Activated(object sender, System.EventArgs e)
{
StringDictionary dataDict = new StringDictionary();
dataDict.Add("Item1", "2001" );
dataDict.Add( "Item2", "2002" );
dataDict.Add( "Item3", "2003" );
}

Now, I want to 'extract' the Value associated with the Key, as follows:

string myValue = dataDict.Item( "Item3" );

This doesn't work .... how should I do this? [The online example
iterates through the entire StringDictionary using the Iiterator; surely
there's a better way....)


Use:

string myValue = dataDict["Item3"];
 
J

John

Thanks for the suggention, Bill.

However, the Item method just doesn't seem to work for me...

How would I use the 'ContainsKey' method? As I understand it, it would
confirm that the entry 'Item2' as a key does exist but wouldn't extract
the associated value....

Or am I missing something?

Thanks again!

John F.

William said:
Use a Hashtable ;-). However, you can use the ContainsKey Method...or the
ContainsValue...

Also, have you tried using the Item method.

string myValue = dataDict.Item("Item3") //that should do it for you..hth

Bill
Hi! I have a StringDictionary itialized as follows:

private void frmICUCalculations_Activated(object sender, System.EventArgs
e)

{
StringDictionary dataDict = new StringDictionary();
dataDict.Add("Item1", "2001" );
dataDict.Add( "Item2", "2002" );
dataDict.Add( "Item3", "2003" );
}

Now, I want to 'extract' the Value associated with the Key, as follows:

string myValue = dataDict.Item( "Item3" );

This doesn't work .... how should I do this? [The online example
iterates through the entire StringDictionary using the Iiterator; surely
there's a better way....)

Thanks for any and all help!

John F.
 
M

Matthew W. Jackson

If you are using C#, you don't use the Item property---you use the indexer
(square brackets directly on the reference). Jon Skeet's example shows you
how to do this.

C# doesn't support named properties with parameters (such as Item(string
key)), and you can only access the default indexer.

If you are using VB, you CAN use Item, but it's not required because Item is
the default indexer.

--Matthew W. Jackson

John said:
Well, it's odd but when I do this I get the compiler error:
'System.Collections.Specialized.StringDictionary" does not contain a
definition for 'Item'

Why this should be is not clear, since clearly 'Item' is a member of
that class....

Help!

Thanks in advance for any elucidation....

John F.

John said:
Hi! I have a StringDictionary itialized as follows:

private void frmICUCalculations_Activated(object sender, System.EventArgs e)
{
StringDictionary dataDict = new StringDictionary();
dataDict.Add("Item1", "2001" );
dataDict.Add( "Item2", "2002" );
dataDict.Add( "Item3", "2003" );
}

Now, I want to 'extract' the Value associated with the Key, as follows:

string myValue = dataDict.Item( "Item3" );

This doesn't work .... how should I do this? [The online example
iterates through the entire StringDictionary using the Iiterator; surely
there's a better way....)


Use:

string myValue = dataDict["Item3"];
 
J

John

Ah, I just found the answer:

The correct syntax is:

string myValue = dataDict["Item3"];

Thanks again for your suggestions...

John F.
Thanks for the suggention, Bill.

However, the Item method just doesn't seem to work for me...

How would I use the 'ContainsKey' method? As I understand it, it would
confirm that the entry 'Item2' as a key does exist but wouldn't extract
the associated value....

Or am I missing something?

Thanks again!

John F.

William said:
Use a Hashtable ;-). However, you can use the ContainsKey Method...or
the
ContainsValue...

Also, have you tried using the Item method.

string myValue = dataDict.Item("Item3") //that should do it for you..hth

Bill
Hi! I have a StringDictionary itialized as follows:

private void frmICUCalculations_Activated(object sender,
System.EventArgs

e)

{
StringDictionary dataDict = new StringDictionary();
dataDict.Add("Item1", "2001" );
dataDict.Add( "Item2", "2002" );
dataDict.Add( "Item3", "2003" );
}

Now, I want to 'extract' the Value associated with the Key, as follows:

string myValue = dataDict.Item( "Item3" );

This doesn't work .... how should I do this? [The online example
iterates through the entire StringDictionary using the Iiterator; surely
there's a better way....)

Thanks for any and all help!

John F.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top