Macro & OOP syntax

Post Reply
hua
Posts: 861
Joined: Fri Oct 28, 2005 2:27 am

Macro & OOP syntax

Post by hua »

I find the following work as expected (just an illustration),

Code: Select all

aOrders := {}
for i := 1 to 7
     cCnt := ltrim(str(i))
     aadd(aOrders, oDbf:order&cCnt)
next
Where in the end, aOrders would contain the value of oDbf:order1, oDbf:order2, oDbf:order3,....oDbf:order7. What annoys me is the reverse isn't true. That is, the following won't work.

Code: Select all

for i := 1 to 7
     cCnt := ltrim(str(i))
     oDbf:order&cCnt := aOrders[i]
next
The values just seem can't be assigned to the database object. Can anyone shed a light on why it's so and a workaround? I'm hoping to avoid having to code it line by line as such

Code: Select all

oDbf:order1 := aOrders[1]
oDbf:order2 := aOrders[2]
oDbf:order3 := aOrders[3]
                .
                .
                .
oDbf:order7 := aOrders[7]
User avatar
xProgrammer
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia

Post by xProgrammer »

My guess would be (based on dim recollections of my C coding days) that the right hand side of the := is evaluated and the left hand side is taken to be in effect the storage location to which the result of that evaluation is stored. So the &cCnt isn't valid on the left hand side of the :=.

One possible workaround would be to use a combination of FieldPos() and FieldPut() in a method inside a method of CLASS Dbf.

Code: Select all

FOR i := 1 to 7
   cCnt := ltrim( str( i ) )
   oDbf:AssignOrder( order&cCnt, aOrders[i] )
NEXT
and inside CLASS Dbf

Code: Select all

METHOD AssignOrder (pName, pValue ) CLASS Dbf
   FieldPut( FieldPos( pName ), pValue )
RETURN nil
That assumes that you want to put the values directly into the (one) table.

I don't know enough about what you are really trying to do to be of more help, but personally I avoid using &. Why can't class Dbf use an array? Or should you have a sub-table? I'm only guessing, but maybe there's a more elegant solution waiting to be found.

Regards
xProgrammer
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post by Antonio Linares »

Hua,

Do it this way:

Code: Select all

for i := 1 to 7 
     cCnt := ltrim(str(i)) 
     OSend( oDbf, "_order" + cCnt, aOrders[i] ) 
next
regards, saludos

Antonio Linares
www.fivetechsoft.com
hua
Posts: 861
Joined: Fri Oct 28, 2005 2:27 am

Post by hua »

xProgrammer wrote: One possible workaround would be to use a combination of FieldPos() and FieldPut() in a method inside a method of CLASS Dbf.
Thanks for the reply Doug :) Yes, that's a valid suggestion but if I'm not mistaken, fieldpos() and fieldput() will read from the physical dbf. I'm trying to optimize the operations as everything is already in an array
xProgrammer wrote: I don't know enough about what you are really trying to do to be of more help, but personally I avoid using &.
I'm in the midst of converting an old dos module to windows. I do avoid '&' like a plague but sometimes if I don't see any alternative I'll end up using it anyway.
xProgrammer wrote: Why can't class Dbf use an array? Or should you have a sub-table? I'm only guessing, but maybe there's a more elegant solution waiting to be found.
Oh it does. The database class that comes with fivewin stores all the field values in oDbf:aBuffers though we access it in the form of oDbf:<fieldname>. This table should've been further normalized as the customers contact details and their orders are currently kept in the same table but for backward-compatibility sake I just maintain the structure as it is.

Here's to hoping to find an elegant solution :)
hua
Posts: 861
Joined: Fri Oct 28, 2005 2:27 am

Post by hua »

Antonio Linares wrote: Do it this way:

Code: Select all

for i := 1 to 7 
     cCnt := ltrim(str(i)) 
     OSend( oDbf, "_order" + cCnt, aOrders[i] ) 
next
Yes! That's it!. Thank you Antonio!
Post Reply