trying with tdatabase

User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: trying with tdatabase

Post by James Bott »

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

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

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.
Last edited by James Bott on Mon Jan 28, 2019 7:16 pm, edited 1 time in total.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: trying with tdatabase

Post by James Bott »

IF oServiziSingoli: EOF ()
cNewId: = strzero (oServiziSingoli: RecNo (), 4)
ELSE
cNewId: = strzero ((VAL (oServiziSingoli: ID) +1), 4)
ENDIF

if james erase this step how calculate the new id ?


You don't need that code if you are using my TAutoincrement class or the autoincrement fieldtype "+".

And the less code you have to write, the better. Less chance of errors, faster, and easier to read. If you use one of the above, you don't have to write any code to get auto-incrementing primary-key values.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
TimStone
Posts: 2955
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Has thanked: 25 times
Been thanked: 2 times
Contact:

Re: trying with tdatabase

Post by TimStone »

James,

You know I agree with you on both points, and that is exactly what I do within my programs.

HOWEVER, I wanted to point out to Silvio just how simple this can be, so I used a VERY generic example.

Tim
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: trying with tdatabase

Post by James Bott »

Tim,

I figured you did. However, that was a good opening for explaining it for everyone else who is following this thread. And I had just written most of it two days ago for someone else.

Thanks for the opening.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
Silvio.Falconi
Posts: 7138
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: trying with tdatabase

Post by Silvio.Falconi »

James Bott wrote:
IF oServiziSingoli: EOF ()
cNewId: = strzero (oServiziSingoli: RecNo (), 4)
ELSE
cNewId: = strzero ((VAL (oServiziSingoli: ID) +1), 4)
ENDIF

if james erase this step how calculate the new id ?


You don't need that code if you are using my TAutoincrement class or the autoincrement fieldtype "+".

And the less code you have to write, the better. Less chance of errors, faster, and easier to read. If you use one of the above, you don't have to write any code to get auto-incrementing primary-key values.



James NOT RUN TAUTOINCREMENTAL I NOT SEE SYSFILE:DBF

WHERE IS the FILE Sysfile.dbf you se eit ? where
Image
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Post Reply