Results 1 to 2 of 2

Thread: [RESOLVED] Reading from and changing information on Text Files

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Resolved [RESOLVED] Reading from and changing information on Text Files

    Hi all.
    For a project I'm currently doing, we need to read four pieces of information (Product name, Quantity, Supplier and Price) from a CSV textfile for a restaurant/bar and change the quantity based on which button the user selected (EG, btnGarlic decreasing the quantity of Garlic Bread). I've been running up against brick walls all week, and just cant work it out. I'm sure what I have at the moment is all over the place, but we've been given very little direction. The way I've done it is also done is a LSV way, but I just don't know how to do it.


    Imports System.IO
    Public Class frmTesting
    Structure Product
    Dim strProduct As String
    Dim intQuantity As Integer
    Dim strSupplier As String
    Dim decPrice As Decimal
    End Structure

    Dim Products(200) As Product
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim X As Integer = 0 'Array counter
    Dim sr As IO.StreamReader = IO.File.OpenText("Food.txt")

    Do Until sr.EndOfStream = True
    Products(X).strProduct = sr.ReadLine
    Products(X).intQuantity = Integer.Parse(sr.ReadLine)
    Products(X).strSupplier = sr.ReadLine
    Products(X).decPrice = Decimal.Parse(sr.ReadLine)
    txtQuantity.Text = Products(X).intQuantity
    X = X + 1

    Loop
    sr.Close()
    End Sub

    Private Sub btnGarlic_Click(sender As Object, e As EventArgs) Handles btnGarlic.Click
    Dim strProduct As String
    Dim intQuantity As Integer
    Dim strSupplier As String
    Dim decPrice As Decimal
    Using sr As IO.StreamReader = New System.IO.StreamReader("FoodAmounts.txt")
    strProduct = sr.ReadLine()
    While (strProduct <> Nothing)
    Products(200).strProduct = sr.ReadLine
    Products(200).intQuantity = Integer.Parse(sr.ReadLine)
    Products(200).strSupplier = sr.ReadLine
    Products(200).decPrice = Decimal.Parse(sr.ReadLine)


    End While
    End Using
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Dim Z As Integer

    Products(Z).intQuantity = Val(txtQuantity.Text = 1)

    Dim sw As IO.StreamWriter = IO.File.CreateText("Save.txt")

    For intCounter As Integer = 0 To 200
    sw.WriteLine(Products(intCounter).strProduct)
    sw.WriteLine(Products(intCounter).intQuantity)
    sw.WriteLine(Products(intCounter).strSupplier)
    sw.WriteLine(Products(intCounter).decPrice)
    Next
    End Sub
    End Class


    Any help would be very very appreciated, thank you.
    Attached Files Attached Files

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: Reading from and changing information on Text Files

    Ok so whats not working? I see you use interger.parse and val. May i ask why do you jump between the Two? One is nearly the correct way the other is not. Please explain why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width