display the average number of books read for all readers in a session

Posted on 2007-09-30
Last Modified: 2008-03-06
I am trying to display the average number of books read for all readers in a session
*The average should be displayed in the summary*

This is how my code reads:

rivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub PointsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PointsToolStripMenuItem.TextChanged

        BonusPointsTextBox.Text = calcPoints(PointsToolStripMenuItem.Text)
    End Sub

    Private Function calcPoints(ByVal input As String) As Integer
        Dim points As Integer

            Dim numBooks As Integer = Integer.Parse(input)

            If numBooks <= 3 Then
                points = numBooks * 10
                points = 3 * 10
                If numBooks <= 6 Then
                    points = points + (numBooks - 3) * 15
                    points = points + (3 * 15)
                    points = points + (numBooks - 6) * 20
                End If
            End If
        Catch ex As Exception
        End Try

        Return points
    End Function
    Friend WithEvents AverageLabel As System.Windows.Forms.Label
    Friend WithEvents AverageTextBox As System.Windows.Forms.TextBox

    Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click

    End Sub
End Class
Question by:qsykes
    LVL 22

    Accepted Solution

    I'm not a VB programmer, but this algorithm will do what you want.
    1. Add two new member variable to the form, or the class that contains your event handlers. Call them mReaders and mTotalBooks, and initialize both to zero.

    2. Inside calcPoints, add this just before you return points:
      mReaders = mReaders + 1
      mTotalBooks = mTotalBooks + numBooks

    3. In PointsToolStripMenuItem_Click, after
    BonusPointsTextBox.Text = calcPoints(PointsToolStripMenuItem.Text)

    add the VB version of this:
     AverageTextBox.Text = ((double)mTotalBooks / (double)mReaders).ToString();

    where (double) is the C# cast operator that converts a numeric value to a double precision floating point operator. You may need to use a format string to limit the precision of the result string.


    Author Comment

    Please close this wuestion. This is not the right solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now