Josh Bloom
Josh Bloom

Reputation: 1

problem with calling functions in visual basic

I've been learning visual basic lately and decided to do a some code using functions,file reading and writelines. the way I'm trying to make it work, the code should be getting information from files I've written and should produce a file after the program itself runs, as well as a messagebox popping up to say that the program is finished. However, I currently can't even get the messagebox to pop up. I'm not sure what I've done wrong, and although I know this specific project is an odd one I've included the code for it below in the hopes someone can help me with it as I can't find any examples that are exactly similar to what I'm attempting. Anyways, any help would be appreciated, thank you!

Imports System.IO
Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Dim fs As New System.IO.FileStream("a9output.txt", IO.FileMode.Create, IO.FileAccess.Write)
        Dim w As New StreamWriter(fs)
        Dim sr As IO.StreamReader = IO.File.OpenText("a8.txt")
        While sr.Peek <> -1
            Dim Strinput As String
            Strinput = sr.ReadLine
            Dim input = Strinput.Split(",")
            Dim age As Double = Val(input(3))
            Dim SD = CalcSD(age)
            Dim Tax = CalcTaxable(SD, age, Strinput)
            w.WriteLine("Name: " & input(0))
            w.WriteLine("Taxable Income:" & Tax)
            w.WriteLine("Age:" & input(3))
            w.WriteLine("Program Completed")
            MsgBox("Program Completed!")
        End While

        sr.Close()
        w.Close()
    End Sub
    Function CalcSD(ByVal age As Integer) As Double
        Dim SD As Double
        If age >= 65 Then
            SD = 17500
        Else
            SD = 12500
        End If
        Return SD
    End Function
    Function CalcTaxable(ByVal SD As Double, age As Double, strinput As String) As Double
        Dim sr As IO.StreamReader = IO.File.OpenText("a8.txt")

        Dim sum As Double
        strinput = sr.ReadLine
        Dim Input() = strinput.Split(",")
        Dim name As Double = Val(Input(0))
        Dim income As Double = Val(Input(1))
        Dim contribution As Double = Val(Input(2))
        sum = income - contribution - SD

        Return sum
    End Function
End Class

Upvotes: 0

Views: 114

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416111

The problem is CalcTaxable() tries to re-open the same file while the button click handler is still using it. Better to worry about all the file/io in one place, and send the values you read to the methods. Also always use Decimal instead of Double when working with money.

Private Sub Button1_Click(sender As Object, e As EventArgs)       
    Using fs As New FileStream("a9output.txt", IO.FileMode.Create, IO.FileAccess.Write), _
          w As New StreamWriter(fs)

        For Each line As String In File.ReadLines("a8.txt")
            'String.Split() is not your friend. A real csv parser would be much better.
            Dim input() As String = line.Split(","c)
            Dim age As Integer = CInt(input(3))
            Dim income As Decimal = CDec(input(1))
            Dim contribution As Decimal = CDec(input(2))
            Dim Tax As Decimal = CalcTaxable(age, income, contribution)

            w.WriteLine($"Name: {input(0)}{vbCrLf}Taxable Income:{Tax}{vbCrLf}Age:{age}")       
        Next
        w.WriteLine("Program Completed")
        MsgBox("Program Completed")
    End Using
End Sub

Function CalcSD(ByVal age As Integer) As Decimal
    If age >= 65 Then Return 17500.0D
    Return 12500.0D
End Function

Function CalcTaxable(age As Integer, income As Decimal, contribution As Decimal) As Decimal
    Return income - contribution - CalcSD(age)
End Function

Upvotes: 1

Related Questions