locked
C# Dictionary RRS feed

  • Question

  • I have a problem when trying to iterate through a KeyValuePair. I get the error:

    Error 1 Cannot convert type 'System.Collections.Generic.KeyValuePair<string,object>' to 'System.Collections.Generic.KeyValuePair<int,MIGV002C.Program.PaymentPlanInformation>' C:\c#\Autolease\Connect\Migration\UI\MIGV002C\Program.cs 298 21 MIGV002C

    The code is here:

    foreach

     

    (KeyValuePair<int, PaymentPlanInformation> kvpPP in dicPaymentPlan)
    {
    if(kvpPP.Key == PaymentPlan)

    {
    Deposit = kvpPP.Value.Deposit;
    Pause = kvpPP.Value.Pause;
    PaymentFrequency = kvpPP.Value.PaymentFrequency;
    }

    }

    PaymentPlanInformation is a struct which contains 3 integers.

    The disctionary has been populated with an integer as the key and a struct to contain the 3 values I need to return.

    Thanks

    Adam.

    Friday, September 4, 2009 11:05 AM

Answers

  • That is because your dictionary is declared as Dictionary<string,object>, then your keys are of this type as well. You can iterate like this:

    foreach (KeyValuePair<int, object> kvpPP in dicPaymentPlan)
    {
        // Check key
        if (kvpPP.Key == PaymentPlan) 
        {
            // Get strong typed value
            PaymentPlanInformation realValue = object as PaymentPlanInformation;
    
            Deposit = realValue .Deposit;
            Pause = realValue .Pause;
            PaymentFrequency = realValue .PaymentFrequency; 
        }
    } 
    
    

    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Proposed as answer by Geert van Horrik Friday, September 4, 2009 11:30 AM
    • Marked as answer by eryang Tuesday, September 8, 2009 3:21 AM
    Friday, September 4, 2009 11:16 AM
  • Hi,

    Why are looping through the dictionay at all, 

    You can do,
     if(dicPaymentPlan.ContainsKey(PaymentPlan))
    {
    PaymentPlanInformation realValue = (PaymentPlanInformation) dicPaymentPlan[PaymentPlan];
            Deposit = realValue .Deposit;
            Pause = realValue .Pause;
            PaymentFrequency = realValue .PaymentFrequency; 
    }

    But as Hans said use Dictionary with your own type than just object.

    Regards,
    Vinil;
    • Marked as answer by eryang Tuesday, September 8, 2009 3:22 AM
    Friday, September 4, 2009 12:20 PM
  • Stupid of me, didn't read that you were using a struct:

    PaymentPlanInformation realValue = (PaymentPlanInformation)object;

    Why are you using a struct instead of a class?


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Marked as answer by eryang Tuesday, September 8, 2009 3:21 AM
    Friday, September 4, 2009 11:23 AM

All replies

  • That is because your dictionary is declared as Dictionary<string,object>, then your keys are of this type as well. You can iterate like this:

    foreach (KeyValuePair<int, object> kvpPP in dicPaymentPlan)
    {
        // Check key
        if (kvpPP.Key == PaymentPlan) 
        {
            // Get strong typed value
            PaymentPlanInformation realValue = object as PaymentPlanInformation;
    
            Deposit = realValue .Deposit;
            Pause = realValue .Pause;
            PaymentFrequency = realValue .PaymentFrequency; 
        }
    } 
    
    

    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Proposed as answer by Geert van Horrik Friday, September 4, 2009 11:30 AM
    • Marked as answer by eryang Tuesday, September 8, 2009 3:21 AM
    Friday, September 4, 2009 11:16 AM
  • this does not compile...

    PaymentPlanInformation realValue = object as PaymentPlanInformation;
    Friday, September 4, 2009 11:23 AM
  • Stupid of me, didn't read that you were using a struct:

    PaymentPlanInformation realValue = (PaymentPlanInformation)object;

    Why are you using a struct instead of a class?


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Marked as answer by eryang Tuesday, September 8, 2009 3:21 AM
    Friday, September 4, 2009 11:23 AM
  • i'm learning and a struct seemed the best (easiest and quickets) way to go?

    Friday, September 4, 2009 11:27 AM
  • I always recommend a class, it isn't that hard to implement:

    	/// <summary>
    	/// Class containing payment plan information.
    	/// </summary>
    	public class PaymentPlanInformation
    	{
    		#region Constructor & destructor
    		/// <summary>
    		/// Creates a new instance of this class.
    		/// </summary>
    		/// <param name="deposit">Deposit.</param>
    		/// <param name="IsPaused">True if the payment plan is paused, otherwise false.</param>
    		/// <param name="paymentFrequency">Payment frequency.</param>
    		public PaymentPlanInformation(double deposit, bool IsPaused, int paymentFrequency)
    		{
    			// Store values
    		}
    		#endregion
    
    		#region Properties
    		/// <summary>
    		/// Gets or sets the deposit.
    		/// </summary>
    		public double Deposit { get; set; }
    
    		/// <summary>
    		/// Gets or sets whether the payment plan is paused.
    		/// </summary>
    		public bool IsPaused { get; set; }
    
    		/// <summary>
    		/// Gets or sets the payment frequency.
    		/// </summary>
    		public int PaymentFrequency { get; set; }
    		#endregion
    	}

    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Friday, September 4, 2009 11:30 AM
  • Don't fix your foreach loop, fix your Dictionary<>.  It should be declared as Dictionary<int, PaymentPlanInformation>.  The compiler says it was declared as Dictionary<string, object>, that's not a very good dictionary.

    Hans Passant.
    Friday, September 4, 2009 12:06 PM
  • That's indeed even better, but I don't know if the user is storing more than only PaymentPlanInformation. Maybe it was a dictionary with several kinds of objects, and the user hoped that by typing the right KeyValuePair, only those objects would come out of the dictionary.

    But indeed, as nobugz says, make your dictionary contain the right type.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Friday, September 4, 2009 12:15 PM
  • Hi,

    Why are looping through the dictionay at all, 

    You can do,
     if(dicPaymentPlan.ContainsKey(PaymentPlan))
    {
    PaymentPlanInformation realValue = (PaymentPlanInformation) dicPaymentPlan[PaymentPlan];
            Deposit = realValue .Deposit;
            Pause = realValue .Pause;
            PaymentFrequency = realValue .PaymentFrequency; 
    }

    But as Hans said use Dictionary with your own type than just object.

    Regards,
    Vinil;
    • Marked as answer by eryang Tuesday, September 8, 2009 3:22 AM
    Friday, September 4, 2009 12:20 PM
  • Don't use structs just because you're learning. In fact, you should stick with classes until you're comfortable enough with them to understand the purpose of structs. Structs are not simpler to implement than classes, they're just different. A lot of people run into problems using structs because they forget they're not working with classes.
    Saturday, September 5, 2009 2:37 AM