Question

Hey everyone i have some code here that bring in one line text from a file and displays into labels. I want to add up eight values from labels and divide by 8 to get the average, and convert that average score into a letter grade that will be displayed in a new label. The code i need help with is in the btnCal procedural.

Here is my code:

   Imports System.IO

   Public Class Form1
    Dim grade As String

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()

     End Sub

     Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles      OpenToolStripMenuItem.Click

    OFDG1.Filter = "Text Files|*.txt|All Files|*.*"
    Dim myResult As DialogResult
    myResult = OFDG1.ShowDialog
    If myResult = Windows.Forms.DialogResult.OK Then
        Dim strReader As StreamReader = File.OpenText(OFDG1.FileName)
        Dim aline As String
        Do Until strReader.EndOfStream
            aline = strReader.ReadLine
            Dim myStuff() = aline.Split(","c)
            lblStudent.Text = myStuff(0)
            lblClassField.Text = myStuff(1)
            lblSemesterInput.Text = myStuff(2)
            picStudent.Image = Image.FromFile(myStuff(3))
            lblInput1.Text = myStuff(4)
            lblInput2.Text = myStuff(5)
            lblInput3.Text = myStuff(6)
            lblInput4.Text = myStuff(7)
            lblInput5.Text = myStuff(8)
            lblInput6.Text = myStuff(9)
            lblMidtermInput.Text = myStuff(10)
            lblFinalInput.Text = myStuff(11)

        Loop
        strReader.Close()
    Else
        MessageBox.Show("You clicked other than OK")

    End If
End Sub

Private Sub btnCal_Click(sender As Object, e As EventArgs) Handles btnCal.Click

    grade = CStr(CInt(lblInput1.Text + lblInput2.Text + lblInput3.Text _
    + lblInput4.Text + lblInput5.Text + _
    lblInput6.Text + lblMidtermInput.Text + lblFinalInput.Text / 8)

    lblFinalLetterGrade.Text = Calculation(CInt(grade))

End Sub
Public Function Calculation(ByVal grade As Integer) As String
    Select Case grade
        Case Is > 89
            Return "A"
        Case Is > 79
            Return "B"
        Case Is > 69
            Return "C"
        Case Is > 59
            Return "D"
        Case Else
            Return "F"
    End Select
End Function
End Class 
Was it helpful?

Solution

You are mixing up concatenation with addition (you can use + for both and, as far as you are not indicating the proper type (integer), VB.NET thinks that you mean strings and just concatenate them. In order to avoid this kind of errors, it is always better using & for concatenation and letting + just for addition) and not putting the proper brackets (division is considered before addition).

grade = CStr((CInt(lblInput1.Text) + CInt(lblInput2.Text) + CInt(lblInput3.Text) _
    + Cint(lblInput4.Text) + CInt(lblInput5.Text) + _
    CInt(lblInput6.Text) + CInt(lblMidtermInput.Text) + CInt(lblFinalInput.Text)) / 8)

In general, it seems like you should work a bit more on properly-structuring your code. Also you are using quite a few methods from old VB (e.g., CStr, CInt); some times there is no problem, other times might drive to confusing codes (e.g., .NET indexing starting always from 0 and certain old functions from 1). On the other hand, if you are programming in VB.NET, why are you using VB6 code at all?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top