Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Static Variables for Grade Counting Program [modified] - SOLVED

Static Variables for Grade Counting Program [modified] - SOLVED

Scheduled Pinned Locked Moved Visual Basic
designhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam Wike
    wrote on last edited by
    #1

    My teacher want's me to design a program that does this. "Design a program that has five buttons, one for each grade. Each time you press a button it increments that grade by one. At the same time it updates a label that displays the current percent of students that passed the exam (grade higher than F)." This is what I have so far...

    Public Class Form1

    Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
        Static intA As Integer = 0
        intA = intA + 1
        lblGradea.Text = intA
    End Sub
    
    Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
        Static intB As Integer = 0
        intB = intB + 1
        lblGradeb.Text = intB
    End Sub
    
    Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
        Static intC As Integer = 0
        intC = intC + 1
        lblGradec.Text = intC
    End Sub
    
    Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
        Static intD As Integer = 0
        intD = intD + 1
        lblGraded.Text = intD
    End Sub
    
    Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click
        Static intF As Integer = 0
        intF = intF + 1
        lblGradef.Text = intF
    End Sub
    

    End Class

    I need help on that part that displays what percentage of students are passing. I assume you have to add together the toal number of grades and then divide by the total number above F and display that in a label. But I cant seem to figure that out...Thanks :-D

    modified on Tuesday, March 30, 2010 10:18 AM

    E S 2 Replies Last reply
    0
    • A Adam Wike

      My teacher want's me to design a program that does this. "Design a program that has five buttons, one for each grade. Each time you press a button it increments that grade by one. At the same time it updates a label that displays the current percent of students that passed the exam (grade higher than F)." This is what I have so far...

      Public Class Form1

      Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
          Static intA As Integer = 0
          intA = intA + 1
          lblGradea.Text = intA
      End Sub
      
      Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
          Static intB As Integer = 0
          intB = intB + 1
          lblGradeb.Text = intB
      End Sub
      
      Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
          Static intC As Integer = 0
          intC = intC + 1
          lblGradec.Text = intC
      End Sub
      
      Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
          Static intD As Integer = 0
          intD = intD + 1
          lblGraded.Text = intD
      End Sub
      
      Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click
          Static intF As Integer = 0
          intF = intF + 1
          lblGradef.Text = intF
      End Sub
      

      End Class

      I need help on that part that displays what percentage of students are passing. I assume you have to add together the toal number of grades and then divide by the total number above F and display that in a label. But I cant seem to figure that out...Thanks :-D

      modified on Tuesday, March 30, 2010 10:18 AM

      E Offline
      E Offline
      Estys
      wrote on last edited by
      #2

      My interpretation of the problem is that every time you press a button, the number of students with that particular grade is incremented. That means that your result must be sum(intA to intE)/ sum(intA to intF).

      1 Reply Last reply
      0
      • A Adam Wike

        My teacher want's me to design a program that does this. "Design a program that has five buttons, one for each grade. Each time you press a button it increments that grade by one. At the same time it updates a label that displays the current percent of students that passed the exam (grade higher than F)." This is what I have so far...

        Public Class Form1

        Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
            Static intA As Integer = 0
            intA = intA + 1
            lblGradea.Text = intA
        End Sub
        
        Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
            Static intB As Integer = 0
            intB = intB + 1
            lblGradeb.Text = intB
        End Sub
        
        Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
            Static intC As Integer = 0
            intC = intC + 1
            lblGradec.Text = intC
        End Sub
        
        Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
            Static intD As Integer = 0
            intD = intD + 1
            lblGraded.Text = intD
        End Sub
        
        Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click
            Static intF As Integer = 0
            intF = intF + 1
            lblGradef.Text = intF
        End Sub
        

        End Class

        I need help on that part that displays what percentage of students are passing. I assume you have to add together the toal number of grades and then divide by the total number above F and display that in a label. But I cant seem to figure that out...Thanks :-D

        modified on Tuesday, March 30, 2010 10:18 AM

        S Offline
        S Offline
        Steven J Jowett
        wrote on last edited by
        #3

        I don't normally do student's homework, but seeing as you have attempted to solve the problem here is some pointers... Remove the lines that declare the STATIC integer and make the PRIVATE member variables e.g.

        Public Class Form1
        Private intA As Integer
        Private intB as Integer
        Private intC as Integer
        Private intD as Integer
        Private intF as Integer

        In the Form_Load Event handler, initiate the starting values e.g.

        intA = 0
        intB = 0

        Now all you need is a Function to return the averages.

        Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

        A 1 Reply Last reply
        0
        • S Steven J Jowett

          I don't normally do student's homework, but seeing as you have attempted to solve the problem here is some pointers... Remove the lines that declare the STATIC integer and make the PRIVATE member variables e.g.

          Public Class Form1
          Private intA As Integer
          Private intB as Integer
          Private intC as Integer
          Private intD as Integer
          Private intF as Integer

          In the Form_Load Event handler, initiate the starting values e.g.

          intA = 0
          intB = 0

          Now all you need is a Function to return the averages.

          Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

          A Offline
          A Offline
          Adam Wike
          wrote on last edited by
          #4

          I figured it out :) Thank you both for steering me in the right direction. Not sure if its the most effcient way of making the program but it does what it's supposed to. Here is the final code...

          'Adam Wike
          'Computer Programming 1
          'Grade Counter
          'Created on March 28, 2010
          'Last Modified on March 30, 2010

          Public Class Form1

          'Declared all variables
          Private intA As Integer
          Private intB As Integer
          Private intC As Integer
          Private intD As Integer
          Private intF As Integer
          Private intE As Integer
          
          'Sets the variables to 0
          Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              intA = 0
              intB = 0
              intC = 0
              intD = 0
              intF = 0
          End Sub
          
          'Sets up the counter and calculates the percentage for each button
          Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
              intA = intA + 1
              lblGradea.Text = intA
              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
              lblPassing.Text = Math.Round(intE, 2) & "%"
          End Sub
          
          Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
              intB = intB + 1
              lblGradeb.Text = intB
              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
              lblPassing.Text = Math.Round(intE, 2) & "%"
          End Sub
          
          Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
              intC = intC + 1
              lblGradec.Text = intC
              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
              lblPassing.Text = Math.Round(intE, 2) & "%"
          End Sub
          
          Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
              intD = intD + 1
              lblGraded.Text = intD
              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
              lblPassing.Text = Math.Round(intE, 2) & "%"
          End Sub
          
          Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click
              intF = intF + 1
              lblGradef.Text = intF
              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
              lblPassing.Text = Math.Round(intE, 2) & "%"
          End Sub
          

          End Class

          S 1 Reply Last reply
          0
          • A Adam Wike

            I figured it out :) Thank you both for steering me in the right direction. Not sure if its the most effcient way of making the program but it does what it's supposed to. Here is the final code...

            'Adam Wike
            'Computer Programming 1
            'Grade Counter
            'Created on March 28, 2010
            'Last Modified on March 30, 2010

            Public Class Form1

            'Declared all variables
            Private intA As Integer
            Private intB As Integer
            Private intC As Integer
            Private intD As Integer
            Private intF As Integer
            Private intE As Integer
            
            'Sets the variables to 0
            Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                intA = 0
                intB = 0
                intC = 0
                intD = 0
                intF = 0
            End Sub
            
            'Sets up the counter and calculates the percentage for each button
            Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
                intA = intA + 1
                lblGradea.Text = intA
                intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
                lblPassing.Text = Math.Round(intE, 2) & "%"
            End Sub
            
            Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
                intB = intB + 1
                lblGradeb.Text = intB
                intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
                lblPassing.Text = Math.Round(intE, 2) & "%"
            End Sub
            
            Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
                intC = intC + 1
                lblGradec.Text = intC
                intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
                lblPassing.Text = Math.Round(intE, 2) & "%"
            End Sub
            
            Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
                intD = intD + 1
                lblGraded.Text = intD
                intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
                lblPassing.Text = Math.Round(intE, 2) & "%"
            End Sub
            
            Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click
                intF = intF + 1
                lblGradef.Text = intF
                intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100
                lblPassing.Text = Math.Round(intE, 2) & "%"
            End Sub
            

            End Class

            S Offline
            S Offline
            Steven J Jowett
            wrote on last edited by
            #5

            adamwike wrote:

            intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100 lblPassing.Text = Math.Round(intE, 2) & "%"

            The above should not be repeated in every button click event. Consider making in a sub-routine for example :

            Private Sub CalcIntE()
            intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100
            lblPassing.Text = Math.Round(intE, 2) & "%"
            End Sub

            Then replace the necessary code in each button click event with CalcIntE for example

            Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnA.Click
            intA = intA + 1
            lblGradea.Text = intA
            CalcIntE
            End Sub

            Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

            A 1 Reply Last reply
            0
            • S Steven J Jowett

              adamwike wrote:

              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100 lblPassing.Text = Math.Round(intE, 2) & "%"

              The above should not be repeated in every button click event. Consider making in a sub-routine for example :

              Private Sub CalcIntE()
              intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100
              lblPassing.Text = Math.Round(intE, 2) & "%"
              End Sub

              Then replace the necessary code in each button click event with CalcIntE for example

              Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnA.Click
              intA = intA + 1
              lblGradea.Text = intA
              CalcIntE
              End Sub

              Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

              A Offline
              A Offline
              Adam Wike
              wrote on last edited by
              #6

              Your right, that is a lot less confusing and makes the program look less cluttered. Thanks!

              S 1 Reply Last reply
              0
              • A Adam Wike

                Your right, that is a lot less confusing and makes the program look less cluttered. Thanks!

                S Offline
                S Offline
                Steven J Jowett
                wrote on last edited by
                #7

                adamwike wrote:

                that is a lot less confusing and makes the program look less cluttered

                Not only that, but more importantly, it is more maintainable.

                Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups