4 Replies - 508 Views - Last Post: 25 January 2013 - 04:34 PM Rate Topic: -----

#1 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

File already open?

Posted 23 January 2013 - 01:57 PM

Imports System.IO

Public Class addCustomer
    Dim intCusNumbers As Integer = 0
    Dim boolAgeVerified As Boolean
    Dim intAgeVerify As Integer


    Sub cusDetailsArrayAdd()
        FileReader = New StreamReader(customersTxt)

        intCusNumbers = FileReader.ReadLine
        ReDim strCusArray(intCusNumbers)

        For intCounter = 1 To intCusNumbers
            strCusArray(intCusNumbers).strCusNum = FileReader.ReadLine
            strCusArray(intCounter).strFirstName = FileReader.ReadLine
            strCusArray(intCounter).strLastName = FileReader.ReadLine
            strCusArray(intCounter).intAge = FileReader.ReadLine
            strCusArray(intCounter).strAddress = FileReader.ReadLine
            strCusArray(intCounter).strTown = FileReader.ReadLine
            strCusArray(intCounter).strPostcode = FileReader.ReadLine
            strCusArray(intCounter).strCusNum = FileReader.ReadLine
        Next

        FileReader.Close()
    End Sub
    Sub AddCustomers()

        FileWriter = New StreamWriter(customersTxt)

        Call increaseCusArray()

        Call AddRecsToFile()

        FileWriter.Close()

    End Sub
    Sub increaseCusArray()
        Dim customerID As Guid = Guid.NewGuid()

        intCusNumbers = intCusNumbers + 1



        ReDim Preserve strCusArray(intCusNumbers)

        strCusArray(intCusNumbers).strCusNum = intCusNumbers
        strCusArray(intCusNumbers).strFirstName = txtFirstName.Text
        strCusArray(intCusNumbers).strLastName = txtLastName.Text
        strCusArray(intCusNumbers).intAge = txtAge.Text
        strCusArray(intCusNumbers).strAddress = txtAddress.Text
        strCusArray(intCusNumbers).strTown = txtTown.Text
        strCusArray(intCusNumbers).strPostcode = txtPost.Text



    End Sub
    Sub AddRecsToFile()

        FileWriter.WriteLine(intCusNumbers)

        For intCounter = 1 To intCusNumbers
            FileWriter.WriteLine(strCusArray(intCounter).strCusNum)
            FileWriter.WriteLine(strCusArray(intCounter).strFirstName)
            FileWriter.WriteLine(strCusArray(intCounter).strLastName)
            FileWriter.WriteLine(strCusArray(intCounter).intAge)
            FileWriter.WriteLine(strCusArray(intCounter).strAddress)
            FileWriter.WriteLine(strCusArray(intCounter).strTown)
            FileWriter.WriteLine(strCusArray(intCounter).strPostcode)

        Next
    End Sub

    
    Private Sub btnCreateAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateAcc.Click
        intAgeVerify = txtAge.Text

        If intAgeVerify <= 5 Then
            MsgBox("Unfortunately this customer isn't old enough to view this performance.")
            btnCreateAcc.Enabled = False
            boolAgeVerified = False

        Else
            Call AddCustomers()
            MsgBox("Account Created. Customer Account number is: ")
            Me.Close()
            Customers.Visible = True
        End If



    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
        Customers.Visible = True
    End Sub

    Private Sub addCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Computer.FileSystem.FileExists("customers.txt") = True Then
            Call cusDetailsArrayAdd()
        End If
    End Sub
End Class


Here is a customer registration form that works when adding customers. However if you close that form and return to the main menu and then reopen it and add another customer, it says that the file is being used by another process but i can't see why.

The streamreader/writer are declared in my module.

Is This A Good Question/Topic? 0
  • +

Replies To: File already open?

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: File already open?

Posted 23 January 2013 - 02:22 PM

Just looking over your code, I cannot see the specific source of the problem, in the code you have provided.

However, please please please don't use a module to share variables across methods (and, presumably, classes) like you have done, especially IDisposable objects. It's just a complete nightmare, and there is absolutely no reason for it.

You need to refactor your code so, at the very least, your addCustomer class uses local StreamReader and StreamWriter objects, rather than global ones. Then, you need to use a using statement to ensure they are always getting disposed of (if they are not, that will likely cause the problem you are seeing).

For example, the cusDetailsArrayAdd() could look like this:

    Sub cusDetailsArrayAdd()
        Using FileReader As New StreamReader(customersTxt)

            intCusNumbers = FileReader.ReadLine
            ReDim strCusArray(intCusNumbers)

            For intCounter = 1 To intCusNumbers
                strCusArray(intCusNumbers).strCusNum = FileReader.ReadLine
                strCusArray(intCounter).strFirstName = FileReader.ReadLine
                strCusArray(intCounter).strLastName = FileReader.ReadLine
                strCusArray(intCounter).intAge = FileReader.ReadLine
                strCusArray(intCounter).strAddress = FileReader.ReadLine
                strCusArray(intCounter).strTown = FileReader.ReadLine
                strCusArray(intCounter).strPostcode = FileReader.ReadLine
                strCusArray(intCounter).strCusNum = FileReader.ReadLine
            Next

        End Using
    End Sub



EDIT:

You need to do that for every occasion where you use those global FileReader and FileWriter variables, throughout the application, and completely remove those variables from the module.

That should fix the problem, providing the file isn't being used elsewhere (e.g. by another program).

However, even if it doesn't fix the problem, by making those small changes, you have still made your code considerably better, easier to read and reason about, and therefore easier to debug; meaning finding where the actual problem lies will be a lot easier.

This post has been edited by CodingSup3rnatur@l-360: 23 January 2013 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: File already open?

Posted 24 January 2013 - 04:02 AM

Thanks. I did that but it still says its open In another process.

Sent from my HTC One X using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: File already open?

Posted 24 January 2013 - 11:42 AM

Can you post your updated code, and any other code where you read from/write to/create the file in question, please? You are likely still not properly disposing of all the objects that read from/write to/create the file, but I cannot say anything for definite without seeing your code.
Was This Post Helpful? 0
  • +
  • -

#5 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Re: File already open?

Posted 25 January 2013 - 04:34 PM

View PostCodingSup3rnatur@l-360, on 24 January 2013 - 11:42 AM, said:

Can you post your updated code, and any other code where you read from/write to/create the file in question, please? You are likely still not properly disposing of all the objects that read from/write to/create the file, but I cannot say anything for definite without seeing your code.



Thanks for your help but i fixed it. Look at line 16 and 23. I had the same lines here, when i should of only had the one on line 16 and i hadn't gotten strCusNum to have a value yet. As i was still working on that. Just removing those lines fixed it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1