New topic: Listbox loop and calculations
<http://forums.realsoftware.com/viewtopic.php?t=46047> Page 1 of 1 [ 2 posts ] Previous topic | Next topic Author Message McDian Post subject: Listbox loop and calculationsPosted: Mon Nov 26, 2012 3:35 pm Joined: Fri Sep 14, 2007 5:00 pm Posts: 378 Hello, been a while since I have had a question I can't resolve on my own. This is a pretty simple one (uh-huh!) in that I am looping through a listbox to obtain a value with the intention of deriving the average. // Preliminary code... ' :-- NONE DEFINED --: // Variable declarations... Dim aUpd, dUpd As Double Dim iCnt As Integer = 0 Dim stUpd As String // Function(s)... For iCnt = 0 To lstMetHist.ListCount-1 aUpd = aUpd + CDbl(lstMetHist.Cell(lstMetHist.ListIndex, 3)) Next dUpd = aUpd / iCnt stUpd = CStr(Format(dUpd, "#.000")) ' Use this to show values accumulated... MsgBox stUpd + " --- " + CStr(Format(aUpd, "#.000")) + " --- " + CStr(iCnt) It's not working correctly in that the aUpd variable doesn't appear to be adding correctly. I haven't been able to determine the exact pattern yet but suffice to say the answers are not correct. I am stumped as I was pretty sure I have is designed correctly. Where am I going off the farm? Top markwalsh Post subject: Re: Listbox loop and calculationsPosted: Mon Nov 26, 2012 3:46 pm Joined: Wed May 20, 2009 4:43 pm Posts: 907 McDian wrote:Hello, been a while since I have had a question I can't resolve on my own. This is a pretty simple one (uh-huh!) in that I am looping through a listbox to obtain a value with the intention of deriving the average. // Preliminary code... ' :-- NONE DEFINED --: // Variable declarations... Dim aUpd, dUpd As Double Dim iCnt As Integer = 0 Dim stUpd As String // Function(s)... For iCnt = 0 To lstMetHist.ListCount-1 aUpd = aUpd + CDbl(lstMetHist.Cell(lstMetHist.ListIndex, 3)) Next dUpd = aUpd / iCnt stUpd = CStr(Format(dUpd, "#.000")) ' Use this to show values accumulated... MsgBox stUpd + " --- " + CStr(Format(aUpd, "#.000")) + " --- " + CStr(iCnt) It's not working correctly in that the aUpd variable doesn't appear to be adding correctly. I haven't been able to determine the exact pattern yet but suffice to say the answers are not correct. I am stumped as I was pretty sure I have is designed correctly. Where am I going off the farm? I believe you want this: aUpd = aUpd + CDbl(lstMetHist.Cell(iCnt, 3)) You were using 'ListIndex' instead of your row counter variable. _________________ RB 2009r4 Windows XP Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 2 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
