0

I have a problem relating to arrays in Windows Form Application. The problem is that I don't know if it's possible to do math with them, and if so how?

Let's say that I have declared something like this int[] price = new int[] { 10, 12, 7, 11 }; What I want to do with these arrays is to add them together depending on if the required checkbox is checked.

This is my form (godisForm) '

Let's say if I check the first and third checkbox, the first and third array should add up.

I want the sum of the checked arrays where it says "Kostar : [0]". The output label is named: kostarLbl.

I have tried it myself, but I know far too little to acually solve it :(

If you have any questions, feel free to ask them!

8
  • 2
    Look at linq Sum() and then post what you have tried. Commented May 22, 2014 at 14:50
  • 1
    Showing your UI does nothing for us, we need to see the code that drives the UI. Commented May 22, 2014 at 14:51
  • I don't really have anything behind the UI except for the if loop that requires you to atleast check one checkbox Commented May 22, 2014 at 14:53
  • 1
    Can't you do a mere foreach loop? D: int total = 0; foreach (int i in price) total += i; Kostar.Value = total; Commented May 22, 2014 at 14:54
  • Hi Hugo - welcome to StackOverflow! You will find that when posting questions, you will be far more likely to receive answers from other users when you post the code that you have tried or the research that you have done, as it shows us that you have attempted to put the work in to solve the problem yourself, before asking us to do it for you. Good luck, and happy coding! :) Commented May 22, 2014 at 14:55

3 Answers 3

1

It is a bit hard to answer this without seeing your code, but I think what you are asking is that you need to add up only certain elements of the array, depending on which checkboxes are checked.

To do this, on the button click event, you would have something like this:

int total = 0;
if(checkbox1.checked){
    total += price[0];
}
if(checkbox2.checked){
    total += price[1];
}
kostarLbl.Text = total.toString();
etc...
Sign up to request clarification or add additional context in comments.

2 Comments

This is very close to what I need, but I need to be able to add multiple arrays up (depending if more than one is checked). UPDATE: Never mind, figured it out! Thank you :)
What you can do then, is use the code I provided to check if the checkbox is checked, and then use the code to sum arrays from the previous answers on the specific arrays associated with that certain checkbox.
1

Declare a global variable which will represent total sum.

int sum = 0; //global variable for whole sum

And declare arrays for each option, for example:

int[] CheckBoxNameArray = new int[] { 10, 12, 7, 11 }; //as your price array, but yo uhave 4 such arrays

Then add for each checkbox such an method handling checked changed event. (just double click on checkbox in editor)

private void CheckBoxName_CheckedChanged(object sender, EventArgs e)
{
    if(this.CheckBoxName.Checked)
    {
        sum += CheckBoxNameArray.Sum();
    }
    else 
    {
        sum -= CheckBoxNameArray.Sum();        
    }
    kostarLbl.Text = sum.ToString();
}        

1 Comment

This is also a good method if you wanted to automatically change the label without having to press a button to do the calculation.
1

As mentioned in the above comment, LINQ provides some useful extension methods on Lists and collections. You will need a reference to the System.Linq namespace if I am not mistaken.

You could call the .Sum() function on your price array to get the sum of the items in your array:

 int[] price = new int[] { 10, 12, 7, 11 };

 if (chkKostar.Checked) {
      var total = price.Sum();
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.