Step 1: Open the data object with one line
oDbf := tdata():new(, "cust" )
Actually, I don't recommend the above. You should have a class for each database. It can be as simple as this:
- Code: Select all Expand view
- Class TCustomers()
Method New()
Endclass
Method New( lShared ) Class TCustomers
Default lShared := .T.
::super():new(,"cust",,lShared)
if ::use()
::SetOrder(1) // primary key
endif
Return Self
You can even make the above smaller, but I won't get into this right now.
Also, I would never name the database object "oDBF"--that doesn't tell you much. More on this below.
Write Each Piece of Code Only Once
You should try to write each piece of code only once. So, now all the basic code to open a file is in one place. You can change the DBF filename by changing it in only once place. You can change the order of the primary-key in only one place. You could even change the type of database in only one place.
The most important reason is so that later when you want to add methods to the class, you already have a basic class to add them to. This prevents you from having to go through all your code later to change the syntax that creates the database object.
I also strongly advocate using a plural name for databases, so you can use a single name for records (e.g. oCustomers and oCustomer).
So now you can do:
- Code: Select all Expand view
- oCustomers:= TCustomers():New()
oCustomers:Print()
oCustomers:CountPastDue()
oCustomer:= TCustomer():New( oCustomers )
oCustomer:Print()
oCustoemr:BalanceDue()
oCustomer:isLate()
oCustomer:Phone := "619-889-7786"
oCustomer:Save()
By using the name of the real-world object that the code object represents, the code is self-documenting. It becomes very easy to read and understand.