New to the object orientated method of doing things and was just wondering what is the suggested method for doing this.
in my first question I asked about parsing a text file, and now with some good ideas (thank you guys) I am starting to write this part of the program.
the text file has multiple sections and multiple data types needed to be read in. This is going fine and as the complexity of the text file grows so the I am changing the code to adapt. But I will end up with 30 or so different variables/arrays/list etc that at least some will need to be global available to the program.
So I now have a chunk of code that at the moment is a method within the class that contains “main”, and all the variables are global. (not nice I know).
I was thinking there must be a neater way to do this, I was thinking use a separate class, create an instance of this class and call the method with in this class that parses the text and populates the variables. Then simple call the class variables (any ones deemed to be public)from with in any other class I need to.
But then what do I know 🙂 just looking for some ideas so i don’t go to far down the road and have to turn back.
Cheers
Your instincts are good; the task of parsing a particular type of text file should be encapsulated in a class that is given some information about where to get the data, and produces an object graph representing the parsed data. As far as the input, a string with the filename is fine; better yet, pass in a Stream or StreamReader; the same parser can then digest the information from a file, memory block or network transmission.
Definitely break things down into repeatable chunks; if a file consists of a lot of similar lines that are parsed into subobjects, create a method to parse a single line and return the subobject.
Also make things as single-purpose as possible; if you have to deal with two types of text files, create one class for each file type, making them implement the same interface if possible. Then, you can implement what’s called a Strategy pattern, where one object’s job is to determine which of a series of other classes should be used to perform a task depending on the specifics.