473,385 Members | 1,925 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,385 software developers and data experts.

Getting a value out of StringDictionary

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.

Nov 15 '05 #1
6 7475
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
"John" <jf*****@pol.net> wrote in message
news:7rnIb.705324$Fm2.607422@attbi_s04...
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.

Nov 15 '05 #2
John <jf*****@pol.net> wrote:
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"];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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.
Jon Skeet [C# MVP] wrote:
John <jf*****@pol.net> wrote:
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"];


Nov 15 '05 #4
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 Ryan wrote:
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
"John" <jf*****@pol.net> wrote in message
news:7rnIb.705324$Fm2.607422@attbi_s04...
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.



Nov 15 '05 #5
Ah, I just found the answer:

The correct syntax is:

string myValue = dataDict["Item3"];

Thanks again for your suggestions...

John F.

John wrote:
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 Ryan wrote:
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
"John" <jf*****@pol.net> wrote in message
news:7rnIb.705324$Fm2.607422@attbi_s04...
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.



Nov 15 '05 #6
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" <jf*****@pol.net> wrote in message
news:RX6Jb.713043$HS4.5107782@attbi_s01...
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.
Jon Skeet [C# MVP] wrote:
John <jf*****@pol.net> wrote:
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"];

Nov 15 '05 #7

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

Similar topics

8
by: VK | last post by:
Hi! What I'm missing in following code? Cannot get the values of radiobuttons. Starting only one class (GetVariant), it works. When I put two classes together, it doesn't. Regards, VK from...
3
by: Peter | last post by:
Hi all, Sorry if this is an obvious/common question, but I'm trying to get the value of a radio button using JavaScript. Where I can easily get the value of a text field: ...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
6
by: melanieab | last post by:
Hi, Easy question. It seems to me that I'm following the examples correctly, but apparently I'm not. I'm trying to retrieve the data from a row called "row" and a column called "File". This is...
5
by: velu | last post by:
Problem in getting value from textbox & radiobuttonlist to a valuable inside a datagid. I want to insert a record into a table thru datagrid. Here is the code (see below) Private Sub...
2
by: Kevin Walzer | last post by:
I'm trying to construct a simple Tkinter GUI and I'm having trouble with getting the value of an entry widget and passing it onto a callback function. But I'm not grokking variable scope correctly....
3
by: daveyand | last post by:
hey, I know in the new browsers you're not able to change the value of the status bar. Which is fair enough, i understand why and agree. Although, i cant seem to get the value of the status...
4
by: bushi | last post by:
hi ! i have following code to display some text on a web form,after getting it from database. <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate>...
3
by: koneru9999 | last post by:
i have one storedprocedure in database.from that i can get last login time .i fetched that value and assigned that value to session in one servlet class.from that session i am getting that last login...
1
by: renatois | last post by:
Getting value from GridView Hi guys, I have a GridView and I need to access a value of an ID field on an event. I converted this field into template and the name of the label control in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.