0

I am trying to calculate the total for a list in my C# program, I have gotten help from several people in my class and we can't seem to find the problem, my code is,

        int totalB = 0;

        Cards.ForEach(delegate(ConsoleApplication1.Program.CreditCard Balance)
        {
            totalB= totalB + Balance;
        });

The error is this Error 1 Operator '+' cannot be applied to operands of type 'int' and 'ConsoleApplication1.Program.CreditCard'

Any help for this would be much appreciated as I have no idea and neither do the people that tried to help me with this issue

  • where is your list? what is credit card? my guess would be `Cards.Sum(x => x.Balance);` – Sayse Aug 17 '13 at 13:17
  • Try yo use a getter in, or from, your class for the member you want to summarize Just think about, you won't summarize credit cards but you could want to summarize the balance, so you should have a member in your class like CreditCar.Balance in a raw type. Use that member to summarize – Silvano Aug 17 '13 at 13:24
  • 2
    Have you overridden the "+" operator on CreditCard? If not then how can you expect to add an int to a class? – Simon Wilson Aug 17 '13 at 13:31

2 Answers2

3

I'm guessing you have a class like:

partial class CreditCard
{
    public int Balance {get; set;}
}

So following what you have, explicitly, you most likely intended:

int totalB = 0;

Cards.ForEach(delegate(ConsoleApplication1.Program.CreditCard card)
{
    totalB = totalB + card.Balance;
});

This iterates over each item in your Cards collection, and adds the value of the Balance property to totalB. Note I have called the variable card in the delegate to further illustrate what is going on - the delegate will be called once for each item in the collection. Inside, you can pick out the Balance property and add it to totalB.


Note you can also do this in a number of other ways:

  1. Using LINQ:

    int totalB = Cards.Sum(card => card.Balance);
    
  2. Using a lambda expression instead of an explicit delegate:

    int totalB = 0;
    
    Cards.Foreach(card => {totalB += card.Balance;});
    
  3. Using a foreach loop:

    int totalB = 0;
    
    foreach(CreditCard card in Cards)
        totalB += card.Balance;
    

(If you are not familiar with it, the x += y is the same as x = x + y.)

lc.
  • 113,939
  • 20
  • 158
  • 187
  • +1 for pointing out how to directly change OP's code, but its probably worth noting that the linq foreach is evil (per say) – Sayse Aug 17 '13 at 13:43
  • well at the moment my class for credit card is this, public class CreditCard { public string ID; public string Name; public string City; public double Limit; public double Balance; as it needs to be defined like that for showing I have an understand ing of the C# programming level we are working through at the momnt – user2675079 Aug 18 '13 at 10:39
  • Just tried it and it works for that, cheers for the help mate. – user2675079 Aug 18 '13 at 10:52
0

As far as getting sum of list is concerned. it is as simple as (assuming Cards is a list)

 Cards.Sum(x=>x.YourPropertyToSum);

Your Error:

The error is this Error 1 Operator '+' cannot be applied to operands of type 'int' and 'ConsoleApplication1.Program.CreditCard'

you are trying to add an int with ConsoleApplication1.Program.CreditCard (what is this) which is obviously not a type that can be added to int. Hence the error you are getting.

Ehsan
  • 31,833
  • 6
  • 56
  • 65