none
C# | How to get the absolute value of x - every item in a listbox. RRS feed

  • Question

  • So I have a list box full of numbers. Let's say these numbers are:

    1

    54

    68

    99

    I have the mean (average) of them. And I want to get the absolute value of the difference between the mean and all of these numbers. Then I want to place them in list box 2.

    Sorry if I'm doing something wrong, as you can see I am very new to this forum :) 
    Friday, September 29, 2017 12:45 AM

All replies


  • I have the mean (average) of them. And I want to get the absolute value of the difference between the mean and all of these numbers. Then I want to place them in list box 2. 

    With what *exactly* are you having problems? The math? Obtaining the
    absolute value? Putting them all into a listbox? Please be specific.

    - Wayne

    P.S. - See:

    Math.Abs Method
    https://msdn.microsoft.com/library/system.math.abs.aspx

    Friday, September 29, 2017 2:22 AM
  • I have listbox1 FULL of numbers, correct?

    Now I want to get the absolute value of the difference of x - (all numbers in listbox)

    That's my issue, I can't find a way to do that. I cant even get to parsing a listbox.

    Friday, September 29, 2017 11:35 PM
  • I have listbox1 FULL of numbers, correct?

    Now I want to get the absolute value of the difference of x - (all numbers in listbox)

    That's my issue, I can't find a way to do that. I cant even get to parsing a listbox.

    Hi,

    You have a list of numbers. Those numbers are stored in listbox. Your listbox contains a collection of items (your numbers) that you can step through in a loop. Each time you go through the loop you access the next item (your next number) in the list.

    There are several ways to do that, perhaps the simplest is using a "foreach" loop, so

    int total = 0;

    foreach(var item in listbox1.Items) { total += item.Text // this won't work as is you need to convert the text into a number

    }

    // finished your loop, do something your total

    To keep things simple, I've omitted both code to parse a number from your listbox Text property mentiond in the comment // this won't work, and for retrieving a number from the value of a list box item. But the code above should get you started and give you some terms to search for, there's plenty of examples about.

    Saturday, September 30, 2017 12:08 AM
  • Thank you! This gave me an idea and it worked :D
    Saturday, September 30, 2017 12:12 AM
  • Hi Luke Erickson,

    If your question ahs been solved, please mark the solution as answer to close the thread. This will make answer searching easier in the forum and be beneficial to community members as well.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 2, 2017 4:30 AM
    Moderator