memvar

memvar

Postby jds » Mon Jul 13, 2020 3:23 pm

How to convert the name of a memory variable into the name of a field and use the variable name as a field name?
jds
 
Posts: 122
Joined: Sat Dec 05, 2009 12:44 pm

Re: memvar

Postby hmpaquito » Mon Jul 13, 2020 4:28 pm

FIELD->
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: memvar

Postby nageswaragunupudi » Mon Jul 13, 2020 6:59 pm

Interesting test:
Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   PRIVATE Salary := 30

   USE CUSTOMER

   subfunc()


return nil

//----------

function subfunc()

   local salary := 10

   ? salary, field->salary, memvar->salary

return nil
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10640
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: memvar

Postby jds » Mon Jul 13, 2020 9:53 pm

The question is f.e.
select sales
goto top
cproduct:=sales->name

select products
locate for products->cproduct="smart"
jds
 
Posts: 122
Joined: Sat Dec 05, 2009 12:44 pm

Re: memvar

Postby nageswaragunupudi » Tue Jul 14, 2020 1:48 am

Code: Select all  Expand view
  PRIVATE cproduct

   select sales
   goto top
   cproduct:=sales->name

   select products
   locate for products->&cproduct="smart"
 


OR

Code: Select all  Expand view
  local cproduct

   select sales
   goto top
   cproduct:=sales->name

   select products
   locate for products->&(cproduct)="smart"
 


OR

Code: Select all  Expand view
  sales->( dbgotop() )
   select products
   locate for products->&(sales->name)="smart"
   // or
   locate for &(sales->name)="smart"
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10640
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: memvar

Postby jds » Tue Jul 14, 2020 7:44 am

Thank you India but it does not work with the macro ampersand. I tried that already yesterday and I tried it again today but it gives errors by compiling

the real source code is as follows:

select temp2
goto top
calias:=temp2->alias
select temp1
sum all temp1->calias to ntotalias
jds
 
Posts: 122
Joined: Sat Dec 05, 2009 12:44 pm

Re: memvar

Postby nageswaragunupudi » Tue Jul 14, 2020 8:42 am

Code: Select all  Expand view
sum all temp1->&(calias) to ntotalias
 


All the snippets I posted were after testing. If you adopt them correctly, they should work.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10640
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: memvar

Postby jds » Tue Jul 14, 2020 9:43 am

By compiling no problem but while calling the procedure I got the error message:
"error base/1449 syntax error : &" (ampersand)
PS: I am a non professional programmer working for free at the food bank of my home town
jds
 
Posts: 122
Joined: Sat Dec 05, 2009 12:44 pm

Re: memvar

Postby nageswaragunupudi » Tue Jul 14, 2020 4:46 pm

Please try this program as it is in your fwh\samples folder
Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   local cfield, nTotSal

   DBCREATE( "TMP.DBF", {{ "FLD", "C", 10, 0 }}, NIL, .T., "TMP" )
   APPEND BLANK
   FIELD->FLD := "SALARY"
   CLOSE DATA

   USE TMP NEW
   cField := TMP->FLD

   USE CUSTOMER NEW

   SUM ALL CUSTOMER->&( cField ) TO ntotsal

   ? ntotsal

return nil
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10640
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: memvar

Postby jds » Tue Jul 14, 2020 6:03 pm

Hallo,
The customer.dbf file that is within the fwh/samples map does not contain a fld field??
By compiling and launching the program that you suggest, I become an alert saying "Alert 38836300.00"??
jds
 
Posts: 122
Joined: Sat Dec 05, 2009 12:44 pm

Re: memvar

Postby nageswaragunupudi » Tue Jul 14, 2020 6:49 pm

"Alert 38836300.00"?

This alert box show the total as 38836300.00.
That means the program worked correctly.

Now, apply the same logic to your situation.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10640
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: memvar

Postby jds » Wed Jul 15, 2020 1:06 pm

hallo,
Can you try my situation? The compiling is OK but when launching I got an error message

.....
select temp2
goto top

do while .NOT. eof()

calias:=temp2->alias
cnaam:=temp2->naam

select temp1

sum all temp1->&( calias ) to ntotalias

oPrn:say(nRow, 10*fntArial2:nWidth, cnaam, fntArial3)
oPrn:say(nRow, 50*fntArial2:nWidth, STR(ntotalias,3), fntArial1)
nRow += 1.3 * fntArial1:nHeight

select temp2

skip 1

enddo
......
jds
 
Posts: 122
Joined: Sat Dec 05, 2009 12:44 pm

Re: memvar

Postby James Bott » Fri Jul 17, 2020 10:07 pm

JDS,

I am not exactly sure what you are trying to do, but if you use database objects you don't need to use local variables for fields--that is built into the database object.

oCustomer:Name

Is actually a buffer that contains a copy of the data in the NAME field.

Also you never need to use workareas or aliases with database objects.

Here is an example of how to build a database object for a customer database:

Code: Select all  Expand view
CLASS TCustomers from TDatabase
   Method New()
ENDCLASS

Method New( lShared ) Class TCustomers
   Default lShared:= .T.
   ::Super:New(,"cust",,lShared)
   If ::Use()
      ::SetOrder("CUSTNO") // Primary key
      ::Gotop()
   Endif
Return self
// Note that this assumes you have set your databases to automatically open thier indexes when you open a database.


Now you can do:

Code: Select all  Expand view
oCustomer:= TCustomer():New()

msgInfo( oCustomer:name )


Have you read the articles that I provided the links to in your other message thread with the same title as this one? If not, please do.

James
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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 28 guests