6 Replies - 35228 Views - Last Post: 14 March 2010 - 11:42 AM Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Dictionary<string,string> Question

Post icon  Posted 11 January 2008 - 08:45 PM

I have this method which populates a Dictionary<sting,string> (I will only show part as it is propietary. Populating it here:

 public Dictionary<string,string> PopulateSelectedNetwork()
		{
			Dictionary<string,string> net = new Dictionary<string,string>();
			switch (_userNetwork)
			{
				case "Friendster":
					net.Add("key","value");
					net.Add("key2", "value2");
					break;
.......



Then when I try to get the values from the object I just populated (in a different method):

Dictionary<string, string> net = PopulateSelectedNetwork();
>>System.Web.HttpContext.Current.Response.Write(net["key"] +" - " + net["key2"]);



I get a System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary, at the line marked with a >>. What am I missing here?

Is This A Good Question/Topic? 1

Replies To: Dictionary<string,string> Question

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Dictionary<string,string> Question

Posted 11 January 2008 - 11:00 PM

I had no problem with this. You did remember to return net from the method right? Are you sure your switch is firing the value that adds keys? Did you make sure that _userNetwork was indeed "Friendster"? If your switch is failing, then you won't add keys and hence it is an empty dictionary. Here is the test case I did with no problems.

private void button1_Click(object sender, EventArgs e)
{
	 Dictionary<string, string> net = PopulateSelectedNetwork();
	 MessageBox.Show(net["key"] + " - " + net["key2"]);
}

public Dictionary<string, string> PopulateSelectedNetwork()
{
	 Dictionary<string, string> net = new Dictionary<string, string>();
	 string _userNetwork = "Friendster";

	 switch (_userNetwork) {
		  case "Friendster":
			   net.Add("key", "value");
			   net.Add("key2", "value2");
			   break;
	 }
	 return net;
}


As you can see I just rigged your setup to make sure the switch adds the keys and then I returned the dictionary.

I then print out a messagebox to show the values were found. No problems. :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Dictionary<string,string> Question

Posted 11 January 2008 - 11:44 PM

I apologize for the mistake, this should be in the ASP.Net Forum so Im moving it now. Martyr2 this still isn't working for me, PM me with your email (if you don't mind) and Ill send you the entire offending code)
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Dictionary<string,string> Question

Posted 12 January 2008 - 12:03 AM

Here at DIC we like to share problems and answers on the board. That way everyone learns. haha ;)
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Dictionary<string,string> Question

Posted 12 January 2008 - 12:16 AM

Hey man, from the code you sent me, you realize that your switch is using _usernetwork, not the parameter "name" that is being used in the function.

So your switch should be switch (name) { should it not? Otherwise why are you passing in the parameter "name"?

My guess is that it doesn't know what _usernetwork is (because you should be using the parameter you passed in) it is tripping off your try catch, which then you clear the selected = null which then returns an empty dictionary.

:)

This post has been edited by Martyr2: 12 January 2008 - 12:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Dictionary<string,string> Question

Posted 14 January 2008 - 01:58 PM

Martyr2, I didn't even notice that! That and making AutoPostBack of the DropDownList containing the networks fixed the problem!

Thanks a ton!
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Rajeev*


Reputation:

Re: Dictionary<string,string> Question

Posted 14 March 2010 - 11:42 AM

That's great
Was This Post Helpful? 0

Page 1 of 1