5 Replies - 727 Views - Last Post: 16 October 2013 - 07:28 PM Rate Topic: -----

#1 MP10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 13

Having trouble showing GPA averages...

Posted 16 October 2013 - 01:38 PM

Hello everyone =)

I am new to the forum and my first post happens to be a issue that I am having with a GPA calculator. I am tasked to create a program that the user to enter a gender (F or M) and GPA for any number of students. The application should calculate the average GPA of All students (F and M) and the GPA of all Females and all Males. The UI has a list box that contains GPAs 1.0 to 4.0 with increments of .1. Also the way to enter Female or Male by using radio boxes.

My problem is that I can not display the averages in the labels that are contained in a groupbox. I have gone through my text box and I find examples that work. Sadly, they do not seem to work for me. Here is my code:


Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' to close the program
        Me.Close()
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' fill the list box with GPA values

        For dblGPA As Double = 1 To 4.1 Step 0.1
            lstGPA.Items.Add(dblGPA.ToString)
        Next dblGPA
    End Sub

    Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        ' Calculates the avg. of 20 students

        Dim dblGPA As Double
        Dim lblFemale As Double
        Dim lblMale As Double
        Dim lblAll As Double

        Double.TryParse(lstGPA.SelectedItem.ToString, dblGPA)
        If radFemale.Checked = True Then
            lblFemale = dblGPA + 10
            lblFemale = lblFemale / 10
        Else
            If radMale.Checked = True Then
                lblMale = dblGPA + 10
                lblMale = lblMale / 10
            End If
        End If
       End Sub

    
End Class


The btnCalc_Click isn't complete. I am just trying to get something to show in the labels.

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble showing GPA averages...

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Having trouble showing GPA averages...

Posted 16 October 2013 - 01:59 PM

This is VB.NET, not VB6. Moving.

What is the exact problem you are having.

I assume you receive error messages, so please post them, together with confirmation as to which lines in your posted code they refer to.

(I suspect you've even got some wavy lines under your code, indicating errors and possibly offering solutions.)

lblFemale = dblGPA + 10

You need to set the Text of labels, and set them to a string not a combination of a string and/or a number:

  • Work with numbers, converting from strings if necessary.
  • Perform calculations on these numbers.
  • Convert the results (numbers) to strings so that you can set them as the Text of your Labels.

Was This Post Helpful? 0
  • +
  • -

#3 MP10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 13

Re: Having trouble showing GPA averages...

Posted 16 October 2013 - 02:31 PM

Well I am not getting any errors when debugging. When I click the calculate button nothing happens. No results are being displayed in the labels. I have looked through my text book and thought it gives examples and they do work. I am wondering if I haven't entered the right command. Sadly, I am having trouble seeing it. I will try what you said. And I hope to replay again with my findings. Thanks Andrew =)
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,022
  • Joined: 03-December 12

Re: Having trouble showing GPA averages...

Posted 16 October 2013 - 03:53 PM

View PostMP10, on 16 October 2013 - 02:31 PM, said:

Well I am not getting any errors when debugging. When I click the calculate button nothing happens. No results are being displayed in the labels. I have looked through my text book and thought it gives examples and they do work. I am wondering if I haven't entered the right command. Sadly, I am having trouble seeing it. I will try what you said. And I hope to replay again with my findings. Thanks Andrew =)



You aren't displaying anything because you are not telling anything what to display. Naming a double "lblFemale" makes most believe that it is a label not a double. Pick the label you want to display the result into and use something like:

labelResultFemale.text = lblFemale.tostring
Was This Post Helpful? 0
  • +
  • -

#5 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 332
  • Joined: 14-November 12

Re: Having trouble showing GPA averages...

Posted 16 October 2013 - 05:46 PM

by looking through your code.
i cant see any line of code(s) that could/should display the calculated result.

you must have a label control on your form and set its text value to the calculated result.
you can drag a label from the controls on the left side of your design window.

the name it lblAverage or anything you'd like to name it.
then the code should look like this.

 Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        ' Calculates the avg. of 20 students
 
        Dim dblGPA As Double
        Dim lblFemale As Double
        Dim lblMale As Double
        Dim lblAll As Double
 
        Double.TryParse(lstGPA.SelectedItem.ToString, dblGPA)
        If radFemale.Checked = True Then
            lblFemale = dblGPA + 10
            lblFemale = lblFemale / 10
        Else
            If radMale.Checked = True Then
                lblMale = dblGPA + 10
                lblMale = lblMale / 10
            End If

        End If
        'this is where we set the label value with the calculated average
        lblFAverage.Text = lblFemale.ToString
        lblMAverage.Text = lblMale.ToString
        'or you can just put both the result in 1 label by doing like this.
        lblAverage.Text = "Female Average: " & lblFemale.ToString & " Male Average: " & lblMale.ToString
       End Sub


and i also suggest you make practice of prefixing your variable names that corresponds to its type like:
Dim dblMaleAverage As Double
Dim strName As String


in order to avoid confusion when debugging your codes.
hope that makes sense.
Good Luck and Happy Coding.
Was This Post Helpful? 0
  • +
  • -

#6 MP10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 13

Re: Having trouble showing GPA averages...

Posted 16 October 2013 - 07:28 PM

Oh thank jimzcode, astonecipher, and andrewsw for your help! With all the inputs I got the code to work! If I run into anymore trouble I diffidently re post. As of right now I just have to add a radio clear function and figure out how to make the math workout. I'll post my final code when finished. =) Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1