sqlvisual-foxprofoxpro

FoxPro How To Correctly Update a Variable Based On Table Values


I am writing a FoxPro program which is accessing multiple tables and updating some of their values after running values through equations. I am trying to store the result of an equation in a variable (set) as such:

b = table1.base
ser = table2.load*b

Despite not showing some of the surrounding code, this should return a positive numeric value, however it just returns 0.0. What I have noticed is that if I print this quantity,

?table2.load*b

it indeed prints the correct result. How come this correct result is not being stored in the variable ser ? Thanks for any help.


Solution

  • The following code would reproduce your observation, and the solution to cure the spooky symptom would be FoxPro's peculiar m. prefix which clarifies that a certain name expression is a variable as opposed to an (alias.)column. Don't hesitate to post back if your scenario / reason / repro is somehow different

    CLEAR 
    CREATE CURSOR table1 (base Int)
    INSERT INTO table1 VALUES (15)
    CREATE CURSOR table2 (load Int)
    INSERT INTO table2 VALUES (6)
    
    LOCAL b, ser
    b = table1.base
    
    CREATE CURSOR SomethingElse(b Int Null)
    INSERT INTO somethingElse VALUES (0)
    ser = table2.load * b
    ? ser && prints 0
    
    ser = table2.load * m.b && the m. tells FoxPro that b is a variable as opposed to an alias row/column
    
    ? m.ser && prints 90