The correct way to work with .txt files in object oriented programming

The program I'm working with works with several different .txt files to store its data. Let's just say that our file looks like this:

a1; b1; c1; 
a2; b2; c2; 
...

      

To work with such a file, I created a class, let's say it's called CFile. Now I have written a function to get each of the vectors a, b and c. They work like this:

Public Shared Function getAFromFile() As List(Of Double)
        Dim datei As New FileStream(filePathSelectedInForm, FileMode.Open)
        Dim leser As New StreamReader(datei)
        Dim zeile As String
        Dim werte() As String

        Dim a As New List(Of Double)
        Dim b As New List(Of Double)
        Dim c As New List(Of Double)
        While leser.Peek <> -1
            zeile = leser.ReadLine
            werte = zeile.Split(";")
            a.Add(werte(0))
            b.Add(werte(1))
            c.Add(werte(2))
        End While

        leser.Close()
        datei.Close()

        getAFromFile = a
    End Function

      

Of course I want to calculate something with the data now. Let's say I want to multiply each Double with a Double of another file named x and then add another r value. So I generate another function in my class something like this:

   Public Shared Function calculateWithA(r as Double) As List(Of Double)
        dim a as List(Of Double) = getAFromFile()
        dim x as List(Of Double) = getXFromFile()
        Dim result As New List(Of Double)
        For i = 0 To a.Count - 1
            result.Add(a.Item(i) * x.Item(i) + r)
        Next
        calculateWithA = result  
   End Function

      

Let's say we have a datagridview on a form and it is filled with our vectors a, b and c. So we have 3 columns and undefined number of lines, depending on the number of elements a, b and c from the text file. Now I add the 4th line where I want to store the calculated values. The correct way is to open my file, get the vector a, open another file, get the vector x, multiply them, add r and then write it to the datagridview.

Now imagine r is the index of the datagridview row. I now need to loop from r = 0 to "number of rows-1" and call the calculation withWithA (r). Each time the function is called functions getAFromFile (), and getXFromFile () is called two in it. I guess opening the file is pretty slow, so I think it would be better to make a public shared list (and Double) and just get them once and then work with them in the calculateWithA function. Something like that:

 Private Shared a As New List(Of Double)
 Private Shared x As New List(Of Double)

 Public Shared sub getAandX()
            a  = getAFromFile()
            x  = getXFromFile()
            Dim result As New List(Of Double)
            For i = 0 To a.Count - 1
                result.Add(a.Item(i) * x.Item(i) + r)
            Next
 End Sub

 Public Shared Function calculateWithA(r as Double) As List(Of Double)
            Dim result As New List(Of Double)
            For i = 0 To a.Count - 1
                result.Add(a.Item(i) * x.Item(i) + r)
            Next
            calculateWithA = result  
       End Function

      

In this case, I can call calculateWithA (r) multiple times, while the file-intensive functions are only called once. But I have to call getAandX () before using calculateWithA (r), or I will get an error or work with the old a and x values. And here's where my problem is: I want my program to be as modular as possible and to follow the rules of an object-oriented program. But if I need to call a function to make sure another function is working, then against these rules, right? But if I work with the modular version, calling every file every time I call this function, I get pretty poor performance and long wait times. What are you guys saying? Which direction should I go or is there a better way to solve this problem?

Thanks in advance!

+3


source to share


1 answer


In this case my guess is that you should call getAandX from calculateWithA so that you check first if the lists are empty or not. If they are empty, call getAandX and then run a loop to get the results. After the loop, clean up the lists a and x as they are no longer useful. Now, every time you call calculateWithA, it first updates the data and then processes it. So the IO oprration file will happen once and the responsibility for loading the data will be done by calculateWithA function.



0


source







All Articles