Well, your result isn't local, in the sense that the function itself is not 
local -- the return value has the same scope as the function.  Memory for a 
record is allocated when the record is declared, says delphi help.  A question 
you might ask is "when is memory for a record freed?".  

If it were me, I would use a tobject instead of a record, and I would use it 
this way:

 function MdbU.GetCurrCustomer(oCustRec: TMyCustRec): boolean;
 begin
   oCustRec.lname := CustRec.Fields['l_name'].Value; 
   oCustRec.fname := CustRec.Fields['f_name'].Value;
 end;


The calling routine would do something like this:

  oCustRec := TMyCustRec.Create;
  if (dbu.GetCurrCustomer(oCustRec) then begin
    .. do stuff with the name ..
  end
  else begin
    .. deal with the error ..
  end;

  ...

  oCustRec.Free;

Jeremy



-----Original Message-----
From: advanced_delphi@yahoogroups.com on behalf of nf4lham
Sent: Sun 6/1/2008 11:07 AM
To: advanced_delphi@yahoogroups.com
Subject: [advanced_delphi] Re: Persistant values shouldn't be
 
Thanks Jeremy -
True, but isn't result local to the routine? I thought it was
destroyed when the method exited and was reinstanced when the method
was entered. 

I made a small app with just those 2 units and result didn't persist,
so I'm confused.

Aside from 
result.lname := '';
how would I clear it?


--- In advanced_delphi@yahoogroups.com, "Jeremy Grand" <[EMAIL PROTECTED]> 
wrote:
>
> You're creating your object in the initialization area; I don't see
where it is cleared.
> 
> jhg
> 
> 
> 
> -----Original Message-----
> From: advanced_delphi@yahoogroups.com on behalf of nf4lham
> Sent: Sat 5/31/2008 7:45 PM
> To: advanced_delphi@yahoogroups.com
> Subject: [advanced_delphi] Persistant values shouldn't be
>  
> I call a method in another unit (dbUnit) to retrieve a record from a
> database.
> 
> On a successful query another method in dbUnit fills a record type to
> be passed back to the calling unit. dbUnit looks like this
> unit dbUnit;
> 
> interface
> uses
>   SysUtils, Classes,  Windows, Variants, DAO_TLB, dialogs, comObj;
> 
> type
>   TMyCustRec = packed record
>     fname: string;
>     lname: string;
> end;
> 
> type
>   TMyDBU = class(TObject)
>     private
>     public
>       function GetCurrCustomer: TMyCustRec;
> end;
> var
>   DBU: TMYDBU;
> 
> implementation
> 
> function MdbU.GetCurrCustomer: TMyCustRec;
> begin
>   result.lname := CustRec.Fields['l_name'].Value; 
>   result.fname := CustRec.Fields['f_name'].Value;
> end;
> 
> initialization
>   DBU := TMYDBU.Create;
> end.
> 
> The call from the main unit is like:
> 
> procedure GetaCustomer;
> var
>   retrec: TMyCustRec;
> begin
>   retrec := DBUnit.DBU.GetCurrCustomer;
> end;
> 
> On subsequent calls, result.lname & result.fname are still populated
> with values from the previous call.They're acting like global
> variables. Why?
> 
> Thanks,
> Mike
>



<<winmail.dat>>

Reply via email to