2

I have following code using class and dictionary that takes state and capital, but I'm unable to retrieve the values by looping through. Need assistance is clearing up the error that occurs in foreach loop.

public class State {
  public string Capital { get; set; }
  public int Population { get; set; }
  public int Size { get; set; }

  public State(string aCapital, int aPopulation, int aSize) {
    Capital = aCapital;
    Population = aPopulation;
    Size = aSize;
  } // Constructor of the class State

  public static Dictionary<string, State> GetStates() {
    Dictionary<string, State> myStates = new Dictionary<string, State>(); // need the () because its a class

    // myStates takes 2 values, one is a string , that is a state and State ,  which is inturn takes 3 values - 
    // Capital,Population, size

    State addStateCapital = new State("Montgomery", 214141, 244);
    myStates.Add("Alabama", addStateCapital);
    // second set
    addStateCapital = new State("Sacramento", 214141, 244);
    myStates.Add("California", addStateCapital);

    return myStates;
  }
}

and my Main program is as follows.. But I get error..

 var theState= State.GetStates();

 // this prints one item in a dictionary

 Console.WriteLine("State Capital of California is " +   theState["California"].Capital);

foreach (KeyValuePair<string,object> entry in theState)
{
    Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.ToString());
}

ERROR on foreach:

Cannot convert keyvaluePair <string,DictionaryDemo.State> to System... Generic KVP<string,object>

Need help in understanding how to correctly retrieve the values.

1
  • It's telling you the error in the error message; you're trying to convert a KeyValuePair<string, State> to a KeyValuePair<string, object>. I suggest just using foreach (var entry in theState). Commented Mar 2, 2019 at 15:29

3 Answers 3

2

1) Use State instead of Object in KeyValuePair<,>. Since you have already specified specified in State.GetStates() return type has

 Dictionary<string, State>

2) Also change the entry.Value.ToString() to entry.Value.Capital. Since it will not show the State Name but the Class Object Name. Therefore, change it entry.Value.Capital

foreach (KeyValuePair<string,State> entry in theState)
 {
 Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.Capital);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

@VajraAyudha let me know how this goes for you.
2

Your GetStates() method returns Dictionary<string, State>, so in this line

var theState= State.GetStates();

the variable theState is assigned to type Dictionary<string, State>. But in the following foreach block you try to access the values of the dictionary as KeyValuePair<string, object>.

So you have to use it like this:

 foreach (KeyValuePair<string, State> entry in theState)
 {
      Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.Capital);
 }

Comments

0

Changing the code snippet to below will solve your problem

foreach (KeyValuePair<string, State> entry in theState)
 {
      Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.Capital);
 }

But to understand why you were getting that error, you may want to take a closer look of definition of Dictionary<TKey, TValue> which is derived from IEnumerable<KeyValuePair<TKey, TValue>>. So when you use foreach loop, internally it acts on IEnumerable<KeyValuePair<TKey, TValue>>. Here compiler expects each entity in foreach to be of type KeyValuePair<TKey, TValue>. Now since in dictionary you have TValue is of type State that why compiler expects KeyValuePair<string, State>. Now to get in-depth understanding on this, you may want to go through "covariant and contravariant concepts c#"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.