Macro & OOP syntax

Macro & OOP syntax

Postby hua » Thu Feb 28, 2008 2:39 am

I find the following work as expected (just an illustration),
Code: Select all  Expand view  RUN
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  Expand view  RUN
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  Expand view  RUN
oDbf:order1 := aOrders[1]
oDbf:order2 := aOrders[2]
oDbf:order3 := aOrders[3]
                .
                .
                .
oDbf:order7 := aOrders[7]
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Postby xProgrammer » Thu Feb 28, 2008 10:00 am

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  Expand view  RUN
FOR i := 1 to 7
   cCnt := ltrim( str( i ) )
   oDbf:AssignOrder( order&cCnt, aOrders[i] )
NEXT


and inside CLASS Dbf

Code: Select all  Expand view  RUN
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
xProgrammer
 
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia

Postby Antonio Linares » Thu Feb 28, 2008 12:39 pm

Hua,

Do it this way:
Code: Select all  Expand view  RUN
for i := 1 to 7
     cCnt := ltrim(str(i))
     OSend( oDbf, "_order" + cCnt, aOrders[i] )
next
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby hua » Fri Feb 29, 2008 1:55 am

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: 1072
Joined: Fri Oct 28, 2005 2:27 am

Postby hua » Fri Feb 29, 2008 1:56 am

Antonio Linares wrote:Do it this way:
Code: Select all  Expand view  RUN
for i := 1 to 7
     cCnt := ltrim(str(i))
     OSend( oDbf, "_order" + cCnt, aOrders[i] )
next


Yes! That's it!. Thank you Antonio!
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], Marc Venken and 34 guests

cron