0

The subtotal should be equal to 9500, and not 9000. I have the code to add a price to a hashset list, but when I click the compute button it's only getting one integer instead of adding them all.

private void computeButton_Click(object sender, EventArgs e)
    {
        double soloPrice = 0.00;
        string selling = Convert.ToString(applianceList.SelectedItem);
        HashSet<double> subTotalPrice = new HashSet<double>(15);
        foreach (string price in itemTag.Items)
        {
            if (selling == "Electric Fan" && !subTotalPrice.Contains(500.00))
            {
                subTotalPrice.Add(500.00 * Convert.ToDouble(itemsNumber.Value));
                soloPrice = 500.00 * Convert.ToDouble(itemsNumber.Value);
            }                   
            else if (selling == "Refrigerator" && !subTotalPrice.Contains(9000.00))
            {
                subTotalPrice.Add(9000.00 * Convert.ToDouble(itemsNumber.Value));
                soloPrice = 9000.00 * Convert.ToDouble(itemsNumber.Value);
            }
            itemPrice.Text = "₱" + soloPrice;
        subTotal.Text = "₱ " + subTotalPrice.Sum();
    }

Program output

Fildor
  • 14,510
  • 4
  • 35
  • 67
dmage
  • 1
  • 1
  • If you have an complete HashSet with every integer in, you can use for loop to iterate over the set to access every element and do the summation – fyb Jul 19 '21 at 06:54
  • It is quite difficult to understand how you are intending for your program to work. You might want to post more code. Normally you would do something like `mySelectedItems.Sum(i => i.Price)`. – JonasH Jul 19 '21 at 07:00
  • 3
    Unrelated: do not represent monetary amounts as floating point types. – Fildor Jul 19 '21 at 07:04
  • You are using `else if` and condition on `selling`'s value, so only one branch will be hit. Always. Because `selling` can only have _one_ of the values. – Fildor Jul 19 '21 at 07:08
  • 1
    ... [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Fildor Jul 19 '21 at 07:11
  • 2
    @Fildor It is _somewhat_ related, since `Contains` on a `HashSet` is going to do equality checks and equality checks on floating point are "problematic". – mjwills Jul 19 '21 at 07:16
  • 1
    @mjwills Excellent point, but it wasn't the reasoning I based the comment on. But yes, considering this, it is indeed related. – Fildor Jul 19 '21 at 07:17
  • Another point to consider: Do you really want to adjust your code anytime there is a new item type available? – Fildor Jul 19 '21 at 08:28

1 Answers1

-2

... get the sum of the list

In your question you talk about a List<T> and in your code you use HashSet<T> - which are different ways to store data and have different advantages. You can read more about them Here at MSDN and Here at stackoverflow

I would go with a List<T> object since you can access it with indexing operator [].

First, I declared a HashSet with type being integer and initialized with 3 values.

HashSet<int> set = new() { 1, 2, 3 };

Then at this code block I used an for loop to iterate over the HashSet and used ElementAt() method to access every element.

I gathered numbers one by one and added them to the summation variable so you can access it later on.

int summation = 0;
for (int i = 0; i < set.Count; i++)
{
    summation = summation + set.ElementAt(i);
}
return summation;

I would suggest you to read about List and use it rather than a HashSet.

fyb
  • 145
  • 13
  • 2
    code-only answers are generally frowned upon. please add some *explanation* as to how and why your solution works. please read [how to write a good answer](https://stackoverflow.com/help/how-to-answer) – Franz Gleichmann Jul 19 '21 at 07:00
  • This is not really any different from just using `.Sum()`, I'm fairly confident the posters problem is somewhere else. – JonasH Jul 19 '21 at 07:03
  • @JonasH using methods provided by LINQ is mostly much slower than going with a simple for loop, that's why I went with a longer method. But as mentioned by other commenters, the question is quite ambiguous – fyb Jul 19 '21 at 07:14
  • 2
    @fybalaban Sure, but the poster only seem to be adding a few items, and the question is how to get the correct result, not to get it faster. – JonasH Jul 19 '21 at 07:48
  • @JonasH In fact, OP only ever adds 1 item because of how he put the conditions. – Fildor Jul 19 '21 at 08:12