0

I am relatively new to coding. I'm trying to figure out why my hours per day aren't converting to numbers that are then added together. Instead they are just taking those strings and writing them as strings and making a huge number.

For example: If I worked 8 hours per day M-F, it displays 88888 instead of 40 (40 hours total for the week). I did convert the strings to a double and then added them together.

Any help is appreciated. I have no teachers to ask on the weekend.

 Public Class Form1
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        ' Get file
        Dim srdFile As System.IO.StreamReader
        srdFile = New System.IO.StreamReader("payroll.txt")

        ' Declarations
        Dim strLine, strRecord() As String
        Dim strFirst As String
        Dim strLast As String
        Dim strPayRate As String
        Dim strMon As String
        Dim strTues As String
        Dim strWed As String
        Dim strThurs As String
        Dim strFri As String
        Dim dblTotalHours As Double
        Dim dblTotalPay As Double
        Dim dblGrandTotalPay As Double


        Do Until srdFile.Peek = -1
            ' Read
            strLine = srdFile.ReadLine
            strRecord = strLine.Split(",")
            strFirst = strRecord(0)
            strLast = strRecord(1)
            strPayRate = Convert.ToDouble(strRecord(2))
            strMon = Convert.ToDouble(strRecord(3))
            strTues = Convert.ToDouble(strRecord(4))
            strWed = Convert.ToDouble(strRecord(5))
            strThurs = Convert.ToDouble(strRecord(6))
            strFri = Convert.ToDouble(strRecord(7))
            ' Processing 
            dblTotalHours = strMon + strTues + strWed + strThurs + strFri
            dblTotalPay = dblTotalHours * strPayRate
            dblGrandTotalPay += dblTotalPay
            ' Output
            rtbOut.AppendText(strFirst & " " & strLast & ControlChars.Tab & "rate: $" & strPayRate.ToString & ControlChars.Tab & "hours: " & dblTotalHours.ToString("n") & ControlChars.Tab & "pay: " &
            dblTotalPay.ToString("c") & vbNewLine & "Total Pay: " & dblGrandTotalPay.ToString("c") & vbNewLine)
        Loop


    End Sub
End Class
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • When you use Convert.ToDouble() then the logical variable type is Double, not String. Now the + operator works the way you'd expected. – Hans Passant Jul 11 '20 at 21:50
  • @HansPassant unfortunately, doing Convert.ToDouble into a string then "+" will still concatenate and not add the values. – Pete -S- Jul 11 '20 at 21:54
  • 2
    That's why it makes sense to declare the variables `As Double`. – Hans Passant Jul 11 '20 at 23:46
  • 1
    If you use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) then Visual Studio can show you where the problem is while you are writing the program. – Andrew Morton Jul 12 '20 at 13:14
  • N.B. 1) Use the Decimal type for money: [Difference between decimal, float and double in .NET?](https://stackoverflow.com/q/618535/1115360) 2) Use the full path to the file, otherwise it might read from a file in a different directory than the one you intended. – Andrew Morton Jul 12 '20 at 16:52

1 Answers1

0

Because your variables are defined as strings, this code is concatenating the values:

strMon + strTues + strWed + strThurs + strFri

In VB.Net you can concatenate using + or &

Convert these variables to a double data type (to preserve the decimal portion):

Dim strMon As Double
Dim strTues As Double
Dim strWed As Double
Dim strThurs As Double
Dim strFri As Double

Then the + operator will add the variables together.

Pete -S-
  • 542
  • 5
  • 13