Search code examples
c#pos

Adding decimals together using buttons


I am creating a POS system using windows forms for a school project and would like to be able to add decimals together using the code in a button and come up with a total as the output at the end... any ideas on how this can be done?


Solution

  • If following doesn't answer your questions, please edit your post by elaborating your question.

    Two questions I get so far.

    1. Add a decimal to an undetermined length array.

    Generally using List in C# if data keeps growing.

    // Put this as your member variable
    List<decimal> m_ItemPriceInOrder = new List<decimal>;
    
    // In your button event
    decimal price = ... // Get your price of item
    m_ItemPriceInOrder.Add( price );
    
    1. To sum up cost of items when checkout.

    Using foreach to iterate through every item in your list. Also a simple for loop is okay, but foreach represents "iterating through every item".

    decimal sum = 0;
    foreach( var item in m_ItemPriceInOrder ){
        sum+=item;
    }