MegaTrain
August 20th, 2004, 05:31 PM
I'm a long-time procedural programmer (VBA, VB6, VB.NET) and am trying to improve my OO skills and figure out how its all supposed to work. I've read a lot of theory, and played with a lot of "book examples", but am having some trouble putting it all into a real project.
Let me give a relatively simple real-world scenario and ask how we would handle this using OO techniques.
"Customer Data Import"
Procedural steps:
1. Open a text file containing new customer data.
2. Open a database containing existing customer data.
3. Loop through the text file, and for each customer:
a. Read the new customer data
b. Look up the customer number in the database
c. Perform a series of calculations based on the data
d. Store the results of the calculation in the database
e. Also log the results in another text file
We also want the UI to display status information (# processed, # found, # not found, etc.)
My first procedural approach used lots of public variables and publically declared objects, and a big giant loop that did all the work. As the calculations got more and more complex, it was nearly unmaintainable.
My second attempt: I have objects for Customer, database, and textfile. I can call customer.read, customer.lookup, customer.calculate, all that stuff. It does segment the code nicely, and keep things in their proper places. I'm still having a couple of problems, though:
1) I still have the need to have stuff referable from anywhere (progress counters is the biggest one). Without using public variables, its much, much harder to update these from all the parts of the code. The counters might need to be updated from one of the classes or subclasses, or the primary Sub procedure. I ended up calling properties on a form directly (which is global in VB6 - probably a no-no) to store this information.
2) I end of passing a lot of objects around as parameters. When I call customer.read() I have to pass the txtFile object as a parameter. When I call customer.Lookup() I have to pass the database object AND the counters object (for keeping track of progress) as parameters. And the customer class has to THEN pass the database object on as a parameter to a bunch of other method calls it makes. Is this the right way to do it?
3) How do I get two classes to talk to each other? I can call a method in one with a parameter, then give back a response, but that doesn't let me pass back MULTIPLE values.
I would be very interested in the approach you might take for a situation like this. One constraint here is that because of the size and type of the database, I only want to open it once and simply use the same database objects anywhere I need it.
Thanks for any thoughts you might contribute!
Let me give a relatively simple real-world scenario and ask how we would handle this using OO techniques.
"Customer Data Import"
Procedural steps:
1. Open a text file containing new customer data.
2. Open a database containing existing customer data.
3. Loop through the text file, and for each customer:
a. Read the new customer data
b. Look up the customer number in the database
c. Perform a series of calculations based on the data
d. Store the results of the calculation in the database
e. Also log the results in another text file
We also want the UI to display status information (# processed, # found, # not found, etc.)
My first procedural approach used lots of public variables and publically declared objects, and a big giant loop that did all the work. As the calculations got more and more complex, it was nearly unmaintainable.
My second attempt: I have objects for Customer, database, and textfile. I can call customer.read, customer.lookup, customer.calculate, all that stuff. It does segment the code nicely, and keep things in their proper places. I'm still having a couple of problems, though:
1) I still have the need to have stuff referable from anywhere (progress counters is the biggest one). Without using public variables, its much, much harder to update these from all the parts of the code. The counters might need to be updated from one of the classes or subclasses, or the primary Sub procedure. I ended up calling properties on a form directly (which is global in VB6 - probably a no-no) to store this information.
2) I end of passing a lot of objects around as parameters. When I call customer.read() I have to pass the txtFile object as a parameter. When I call customer.Lookup() I have to pass the database object AND the counters object (for keeping track of progress) as parameters. And the customer class has to THEN pass the database object on as a parameter to a bunch of other method calls it makes. Is this the right way to do it?
3) How do I get two classes to talk to each other? I can call a method in one with a parameter, then give back a response, but that doesn't let me pass back MULTIPLE values.
I would be very interested in the approach you might take for a situation like this. One constraint here is that because of the size and type of the database, I only want to open it once and simply use the same database objects anywhere I need it.
Thanks for any thoughts you might contribute!