I am creating a recipe program that will allow the user to save recipes in an external file, retrieve them and work out a new value for ingrediants depending on how many people they want this time. However within my code errors keep appearing and I cannot work out why they do. Here is my code, please help if you can;

Imports System.IO
Public Class Form1
    Dim Unit, nameofrecipe, nameofingrediant, oldrecipe, recipewanted, filecontents As String
    Dim quantity, originalamountofpeople, newpeople As Integer
    Dim filename As String
    Dim filewriter As IO.StreamWriter
    Dim filereader As IO.StreamReader
    Dim txt_ As String
    Dim counter As Integer
    Private Sub btnenternameandpeople_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenternameandpeople.Click
        nameofrecipe = (txtnameofrecipe.Text)
        originalamountofpeople = (txtoriginalpeople.Text)
        filename = (CurDir() & txtnameofrecipe.Text & ".txt")
        lstdisplayenteredrecipe.Items.Add(nameofrecipe & " " & "for" & " " & originalamountofpeople)
        lstdisplayenteredrecipe.SelectedIndex = lstdisplayenteredrecipe.SelectedIndex + 1
    End Sub
    Private Sub btnenteringrediant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnenteringrediant.Click
        Unit = txtunit.Text
        quantity = Val(txtquantity.Text)
        nameofingrediant = txtnameofingrediant.Text
        lstdisplayenteredrecipe.Items.Add(nameofingrediant & "," & quantity & "," & Unit)
        lstdisplayenteredrecipe.SelectedIndex = lstdisplayenteredrecipe.SelectedIndex + 1
        txtunit.Clear()
        txtquantity.Clear()
        txtnameofingrediant.Clear()
    End Sub
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Dim i As Integer
        filewriter = New StreamWriter(filename, True) ' the 'true' means it appends the text rather than deletes the old part
        For i = 0 To lstdisplayenteredrecipe.Items.Count - 1
            filewriter.WriteLine(lstdisplayenteredrecipe.Items.Item(i))
        Next
        filewriter.Close()
        txtoriginalpeople.Clear()
        txtnameofingrediant.Clear()
        txtnameofrecipe.Clear()
        txtunit.Clear()
        txtquantity.Clear()
    End Sub
    Private Sub btnnewrecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnewrecipe.Click
        Dim wholeingrediant As String
        Dim ingrediant As String
        Dim originalquantity As Integer
        Dim newquantity As Integer
        Dim oldpeople As Integer = 10
        Dim unit As String
        Dim counter As Integer = 0
        filename = (CurDir() & txtfindoldrecipe.Text & ".txt")
        If My.Computer.FileSystem.FileExists(filename) Then
            filereader = New StreamReader(filename)
            Do
                wholeingrediant = filereader.ReadLine
                Dim line As String() = Split(wholeingrediant, ",")
                counter = counter + 1
                For Each Str As String In line
                    counter = counter + 1
                    If counter = 1 Then
                        ingrediant = Str
                        lstnewrecipe.Items.Add(ingrediant)
                    End If
                    If counter = 2 Then
                        originalquantity = Val(Str)
                        newquantity = originalquantity / oldpeople * Val(txtnewpeople.Text)
                        lstnewrecipe.Items.Add(newquantity)
                    End If
                    If counter = 3 Then
                        unit = Str
                        lstnewrecipe.Items.Add(unit)
                    End If
                Next
            Loop Until line Is Nothing
        Else : MsgBox("This recipe has not been entered before, please enter original quantities on the left of the screen", MsgBoxStyle.Information)
        End If


    End Sub
    Private Sub btnfinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfinish.Click
        filereader.Close()
        ' so the user does not have to come out of the program to enter another recipe
        Me.Close()
    End Sub
End Class

Recommended Answers

All 4 Replies

What error messages are you getting and on what lines?

the line variable u have declared inside ur do while loop and calling until the line variable is nothing....

declare the line variable before the do while loop....

Dim line As String() = Split(wholeingrediant, ",")

Make sure that the user enters only numeric values wherever required....

Basically the main problem is that it can;t successfully split the string, but overall when i run the program and try to read it, ill press the buttons and nothing will come up at all?????

Debug your code and step through it. That will help you find the exact section that is causing problems. And this was really hard to read. You should get in the habit of using either pascal case (PascalCase - capitalise every first letter) or camel case (camelCase - capitalise every first letter after the first word). Variables and methods become a lot easier to read. txtnameofingrediant.Clear() becomes txtNameOfIngrediant.Text for example, much easier for someone else to come along and read (and that includes yourself). Just a tip:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.