none
returning values from a listbox

    Question

  • I am new to VB and I am trying to solve what is probably a simple problem.

    I have a listbox that is populated by parsing a delimited text file. The listbox is filled with temperatures that have been sampled by a sensor every 10 minutes during a day. This gives me 144 samples per day. What i need to find is the following:

    The minimum temperature, the maximum temperature, and the average temperature. If I knew how to retrieve the values (temps.), I am sure I could perform the math. How can I get this information?

    Friday, May 19, 2006 8:44 PM

Answers

  • This is something you should do when populating the listbox. Because you are iterating through the items at that point and you will end up iterating through them again if you use the items in the listbox. I init the min/max values to be the first item in the temperature list because it need to be a value that is in the list.

    This is just a demonstration of how it can be made and you probably need to modify it to suit you.

    Dim maxtemp As Integer = temperaturelist[0]
    Dim mintemp As Integer = maxtemp

    For Each temp in templist
       If temp > maxtemp Then maxtemp = temp
       If temp < mintemp Then mintemp = temp

       ' Add item to listbox
    Next

     

    Friday, May 19, 2006 9:27 PM
    Moderator

All replies

  • This is something you should do when populating the listbox. Because you are iterating through the items at that point and you will end up iterating through them again if you use the items in the listbox. I init the min/max values to be the first item in the temperature list because it need to be a value that is in the list.

    This is just a demonstration of how it can be made and you probably need to modify it to suit you.

    Dim maxtemp As Integer = temperaturelist[0]
    Dim mintemp As Integer = maxtemp

    For Each temp in templist
       If temp > maxtemp Then maxtemp = temp
       If temp < mintemp Then mintemp = temp

       ' Add item to listbox
    Next

     

    Friday, May 19, 2006 9:27 PM
    Moderator
  • I can see how this can work for the min. and max. values.

    However, I can't sort the list in that fashion. It must stay linked to another list that is already sorted chronologically.

    But how do I get an average? Can I add the values and divide by index(+1)?

    Friday, May 19, 2006 9:38 PM
  • What you suggest to get the average should work.
    Friday, May 19, 2006 9:43 PM
    Moderator
  • Thanks a lot. That was the solution.
    Friday, May 19, 2006 11:35 PM