3 Replies - 1759 Views - Last Post: 05 June 2009 - 06:53 AM Rate Topic: -----

#1 Jamnt0ast   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 09

Modifying code to integrate a function procedure

Post icon  Posted 04 June 2009 - 11:19 PM

Hey. I'm trying to modify this program so that a function procedure determines the student's grade. The problem is, I don't even know where to start.

here's my current code:
Option Explicit On 
Option Strict On

Public Class GradeForm
	Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents uiAssignGradeButton As System.Windows.Forms.Button
	Friend WithEvents uiExitButton As System.Windows.Forms.Button
	Friend WithEvents uiIdPtsTotLabel As System.Windows.Forms.Label
	Friend WithEvents uiPointsGradeLabel As System.Windows.Forms.Label
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.uiIdPtsTotLabel = New System.Windows.Forms.Label
		Me.uiPointsGradeLabel = New System.Windows.Forms.Label
		Me.uiAssignGradeButton = New System.Windows.Forms.Button
		Me.uiExitButton = New System.Windows.Forms.Button
		Me.SuspendLayout()
		'
		'uiIdPtsTotLabel
		'
		Me.uiIdPtsTotLabel.AutoSize = True
		Me.uiIdPtsTotLabel.Location = New System.Drawing.Point(24, 24)
		Me.uiIdPtsTotLabel.Name = "uiIdPtsTotLabel"
		Me.uiIdPtsTotLabel.Size = New System.Drawing.Size(176, 23)
		Me.uiIdPtsTotLabel.TabIndex = 2
		Me.uiIdPtsTotLabel.Text = "Total points and grade:"
		'
		'uiPointsGradeLabel
		'
		Me.uiPointsGradeLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
		Me.uiPointsGradeLabel.Font = New System.Drawing.Font("Tahoma", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.uiPointsGradeLabel.Location = New System.Drawing.Point(24, 48)
		Me.uiPointsGradeLabel.Name = "uiPointsGradeLabel"
		Me.uiPointsGradeLabel.Size = New System.Drawing.Size(168, 160)
		Me.uiPointsGradeLabel.TabIndex = 3
		Me.uiPointsGradeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		'
		'uiAssignGradeButton
		'
		Me.uiAssignGradeButton.Location = New System.Drawing.Point(336, 176)
		Me.uiAssignGradeButton.Name = "uiAssignGradeButton"
		Me.uiAssignGradeButton.Size = New System.Drawing.Size(144, 32)
		Me.uiAssignGradeButton.TabIndex = 0
		Me.uiAssignGradeButton.Text = "&Assign Grade"
		'
		'uiExitButton
		'
		Me.uiExitButton.Location = New System.Drawing.Point(488, 176)
		Me.uiExitButton.Name = "uiExitButton"
		Me.uiExitButton.Size = New System.Drawing.Size(80, 32)
		Me.uiExitButton.TabIndex = 1
		Me.uiExitButton.Text = "E&xit"
		'
		'GradeForm
		'
		Me.AcceptButton = Me.uiAssignGradeButton
		Me.AutoScaleBaseSize = New System.Drawing.Size(8, 20)
		Me.ClientSize = New System.Drawing.Size(592, 238)
		Me.Controls.Add(Me.uiExitButton)
		Me.Controls.Add(Me.uiAssignGradeButton)
		Me.Controls.Add(Me.uiPointsGradeLabel)
		Me.Controls.Add(Me.uiIdPtsTotLabel)
		Me.Font = New System.Drawing.Font("Tahoma", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.Name = "GradeForm"
		Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		Me.Text = "Grade Calculator"
		Me.ResumeLayout(False)

	End Sub

#End Region

	Private Sub uiExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiExitButton.Click
		'ends the application
		Me.Close()
	End Sub

	Private Sub uiAssignGradeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiAssignGradeButton.Click
		'calculates the total points earned, displays the total points earned
		'and the grade

		'declare variables
		Dim testNumber As Integer   'counter-keeps track of the number of tests
		Dim totalPoints As Integer  'accumulator-keeps track of the total points
		Dim button As Integer	   'used by the MessageBox.Show method
		Dim testScore As String	 'stores the user's input
		Dim grade As String		 'stores the grade

		Try
			Do
				'initialize total points accumulator
				totalPoints = 0

				'clear label control
				Me.uiPointsGradeLabel.Text = ""

				'get and accumulate test scores
				For testNumber = 1 To 3
					testScore = InputBox("Enter the points earned on test " _
						& testNumber & ":", "Grade Calculator", "0")
					If testScore = "" Then
						Exit For
					Else
						'update the accumulator
						totalPoints = totalPoints + Integer.Parse(testScore)
						'display the points in the uiPointsGradeLabel
						Me.uiPointsGradeLabel.Text = Me.uiPointsGradeLabel.Text _
							& testScore & ControlChars.NewLine
					End If
				Next testNumber

				'assign grade
				Select Case totalPoints
					Case Is >= 270
						grade = "A"
					Case Is >= 240
						grade = "B"
					Case Is >= 210
						grade = "C"
					Case Is >= 180
						grade = "D"
					Case Else
						grade = "F"
				End Select

				'display total points earned and grade
				Me.uiPointsGradeLabel.Text = Me.uiPointsGradeLabel.Text _
					& Convert.ToString(totalPoints) & " -> " & grade

				'determine whether the user wants to calculate the grade for another student
				button = MessageBox.Show("Calculate another student's grade?", _
					"Grade Calculator", MessageBoxButtons.YesNo, _
					MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
			Loop While button = DialogResult.Yes

		Catch exFormat As FormatException
			MessageBox.Show("The test score must be numeric.", _
				"Grade Calculator", MessageBoxButtons.OK, _
				MessageBoxIcon.Information)
		Catch ex As Exception
			MessageBox.Show(ex.Message, "Grade Calculator", _
				MessageBoxButtons.OK, MessageBoxIcon.Information)
		End Try
		
	End Sub

End Class

This post has been edited by Jamnt0ast: 04 June 2009 - 11:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Modifying code to integrate a function procedure

#2 Luc001   User is offline

  • D.I.C Addict

Reputation: 85
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Modifying code to integrate a function procedure

Posted 05 June 2009 - 12:19 AM

Hi,

You can do it this way:

 Private Sub uiAssignGradeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiAssignGradeButton.Click
	   calculategrade()
	   
	End Sub

 Public Function calculategrade()
		'calculates the total points earned, displays the total points earned
		'and the grade

		'declare variables
		Dim testNumber As Integer   'counter-keeps track of the number of tests
		Dim totalPoints As Integer  'accumulator-keeps track of the total points
		Dim button As Integer	   'used by the MessageBox.Show method
		Dim testScore As String	 'stores the user's input
		Dim grade As String		 'stores the grade

		Try
			Do
				'initialize total points accumulator
				totalPoints = 0

				'clear label control
				Me.uiPointsGradeLabel.Text = ""

				'get and accumulate test scores
				For testNumber = 1 To 3
					testScore = InputBox("Enter the points earned on test " _
						& testNumber & ":", "Grade Calculator", "0")
					If testScore = "" Then
						Exit For
					Else
						'update the accumulator
						totalPoints = totalPoints + Integer.Parse(testScore)
						'display the points in the uiPointsGradeLabel
						Me.uiPointsGradeLabel.Text = Me.uiPointsGradeLabel.Text _
							& testScore & ControlChars.NewLine
					End If
				Next testNumber

				'assign grade
				Select Case totalPoints
					Case Is >= 270
						grade = "A"
					Case Is >= 240
						grade = "B"
					Case Is >= 210
						grade = "C"
					Case Is >= 180
						grade = "D"
					Case Else
						grade = "F"
				End Select

				'display total points earned and grade
				Me.uiPointsGradeLabel.Text = Me.uiPointsGradeLabel.Text _
					& Convert.ToString(totalPoints) & " -> " & grade

				'determine whether the user wants to calculate the grade for another student
				button = MessageBox.Show("Calculate another student's grade?", _
					"Grade Calculator", MessageBoxButtons.YesNo, _
					MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
			Loop While button = DialogResult.Yes

		Catch exFormat As FormatException
			MessageBox.Show("The test score must be numeric.", _
				"Grade Calculator", MessageBoxButtons.OK, _
				MessageBoxIcon.Information)
		Catch ex As Exception
			MessageBox.Show(ex.Message, "Grade Calculator", _
				MessageBoxButtons.OK, MessageBoxIcon.Information)
		End Try
		Return totalPoints
	End Function

Was This Post Helpful? 0
  • +
  • -

#3 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

Re: Modifying code to integrate a function procedure

Posted 05 June 2009 - 05:53 AM

My first question is why are you designing in the Form Designer code?
Was This Post Helpful? 0
  • +
  • -

#4 Jamnt0ast   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 09

Re: Modifying code to integrate a function procedure

Posted 05 June 2009 - 06:53 AM

View Postwoodjom, on 5 Jun, 2009 - 04:53 AM, said:

My first question is why are you designing in the Form Designer code?


I'm not. I should have left that part out of my code since it is not the problem.

View PostLuc001, on 4 Jun, 2009 - 11:19 PM, said:

Hi,

You can do it this way:

 Private Sub uiAssignGradeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiAssignGradeButton.Click
	   calculategrade()
	   
	End Sub

 Public Function calculategrade()
		'calculates the total points earned, displays the total points earned
		'and the grade

		'declare variables
		Dim testNumber As Integer   'counter-keeps track of the number of tests
		Dim totalPoints As Integer  'accumulator-keeps track of the total points
		Dim button As Integer	   'used by the MessageBox.Show method
		Dim testScore As String	 'stores the user's input
		Dim grade As String		 'stores the grade

		Try
			Do
				'initialize total points accumulator
				totalPoints = 0

				'clear label control
				Me.uiPointsGradeLabel.Text = ""

				'get and accumulate test scores
				For testNumber = 1 To 3
					testScore = InputBox("Enter the points earned on test " _
						& testNumber & ":", "Grade Calculator", "0")
					If testScore = "" Then
						Exit For
					Else
						'update the accumulator
						totalPoints = totalPoints + Integer.Parse(testScore)
						'display the points in the uiPointsGradeLabel
						Me.uiPointsGradeLabel.Text = Me.uiPointsGradeLabel.Text _
							& testScore & ControlChars.NewLine
					End If
				Next testNumber

				'assign grade
				Select Case totalPoints
					Case Is >= 270
						grade = "A"
					Case Is >= 240
						grade = "B"
					Case Is >= 210
						grade = "C"
					Case Is >= 180
						grade = "D"
					Case Else
						grade = "F"
				End Select

				'display total points earned and grade
				Me.uiPointsGradeLabel.Text = Me.uiPointsGradeLabel.Text _
					& Convert.ToString(totalPoints) & " -> " & grade

				'determine whether the user wants to calculate the grade for another student
				button = MessageBox.Show("Calculate another student's grade?", _
					"Grade Calculator", MessageBoxButtons.YesNo, _
					MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
			Loop While button = DialogResult.Yes

		Catch exFormat As FormatException
			MessageBox.Show("The test score must be numeric.", _
				"Grade Calculator", MessageBoxButtons.OK, _
				MessageBoxIcon.Information)
		Catch ex As Exception
			MessageBox.Show(ex.Message, "Grade Calculator", _
				MessageBoxButtons.OK, MessageBoxIcon.Information)
		End Try
		Return totalPoints
	End Function

Thank you! this is extremely helpful
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1