user1765862
user1765862

Reputation: 14205

Understanding dictionaries, adding new values to the dictionary using c#

If I have Customer object which have Payment property which is dictionary of custom enum type and decimal value like

Customer.cs
public enum CustomerPayingMode
{
   CreditCard = 1,
   VirtualCoins = 2, 
   PayPal = 3
}
public Dictionary<CustomerPayingMode, decimal> Payment;

In client code I have problem with adding values to the Dictionary, tried like this

Customer cust = new Customer();
cust.Payment = new Dictionary<CustomerPayingMode,decimal>()
                      .Add(CustomerPayingMode.CreditCard, 1M);

Upvotes: 4

Views: 389

Answers (5)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

You could initialize the dictionary inline:

Customer cust = new Customer();
cust.Payment = new Dictionary<CustomerPayingMode, decimal>()
{
    { CustomerPayingMode.CreditCard, 1M }
};

You might also want to initialize the dictionary inside of the Customer constructor and let users add to Payment without having to initialize the dictionary:

public class Customer()
{
    public Customer() 
    {
        this.Payment = new Dictionary<CustomerPayingMode, decimal>();
    }

    // Good practice to use a property here instead of a public field.
    public Dictionary<CustomerPayingMode, decimal> Payment { get; set; }
}

Customer cust = new Customer();
cust.Payment.Add(CustomerPayingMode.CreditCard, 1M);

Upvotes: 2

JeffO
JeffO

Reputation: 8053

Add values to your dictionary on a separate line:

    Customer cust = new Customer();
    cust.Payment = new Dictionary<CustomerPayingMode, decimal>();
    cust.Payment.Add(CustomerPayingMode.CreditCard, 1M);

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68440

So far I understand cust.Payment is type of Dictionary<CustomerPayingMode,decimal> but you're assigning it the result of .Add(CustomerPayingMode.CreditCard, 1M).

You'll need to do

cust.Payment = new Dictionary<CustomerPayingMode,decimal>();
cust.Payment.Add(CustomerPayingMode.CreditCard, 1M);

When you chain method calls, the result is the return value of the last call in the chain, in your case, .Add method. Since it returns void, it fails to cast to Dictionary<CustomerPayingMode,decimal>

Upvotes: 1

Daniel Imms
Daniel Imms

Reputation: 50269

You're creating the dictionary, adding a value to it and then returning the result of the .Add function to your variable.

Customer cust = new Customer();

// Set the Dictionary to Payment
cust.Payment = new Dictionary<CustomerPayingMode, decimal>();

// Add the value to Payment (Dictionary)
cust.Payment.Add(CustomerPayingMode.CreditCard, 1M);

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37576

The Add() methode does not return a value which you can assign to cust.Payment, you need to create the dictionary then call the Add() methode of the created Dictionary object:

Customer cust = new Customer();
cust.Payment = new Dictionary<CustomerPayingMode,decimal>();
cust.Payment.Add(CustomerPayingMode.CreditCard, 1M);

Upvotes: 6

Related Questions