Board index » delphi » Before I go Insane Need help with TVarRec and TDateTime

Before I go Insane Need help with TVarRec and TDateTime

D5, W98 SE
All updates Applied.
I have written a function which is supposed to return the record keys
of a table. Why ? so I can specity Table.FindKey(arg)
instead of Table.FindKey([value1, value2,...ValueN])   I am using a
TVarRec to load up the fields values and will use the result of the
function for the parameter in the findKey.  This works great until you
try to load up a TDateTime field. Basically I'm trying to cast a
Double, which is how TdateTime is defined to an extended type.  In
debugging all seems fine. that is the Extended var is loaded. By
observing the Result.vExtended^ the correct value is assigned.  How
ever, upon completion of the function the value dissappears.  On other
words, if you look ad dd:=LoadKeys(table) the value of
dd[0].vExtended^ is gone, (some un godly small number).
and Yes the first field of the table is a TDateTime field.  the code
is as follows:

Type
   TKeyArr=Array of TVarRec;

function LoadKeys(Tabl:TTable):TKeyArr;
Var
 Tmp:TKeyArr;
 TsL:TStringList;
 Nf, I:Integer;
 p:^double;
 e:^Extended;
 Db:TDateTime;
 Ex:Extended;

begin
TSl:=TStringList.Create;
tabl.GetIndexNames(TSl);
Nf:=NumChars(tabl.IndexDefs.Items[0].Fields,';');
SetLength(Result,Nf+1);
SetLength(Tmp,Nf+1);

For I:=0 to Nf do begin

 Case  tabl.Fields[I].DataType of
    ftInteger:begin
              Tmp[i].VType:=VtInteger;
              Tmp[i].vInteger:=Tabl.Fields[i].AsInteger;
              end;

     ftString:begin
              Tmp[i].Vtype:=vtAnsiString;
              AnsiString(Tmp[i].VansiString):=Tabl.Fields[i].AsString
              end;

       ftDate:begin
              Tmp[i].Vtype:=VtExtended;
              db:= Tabl.Fields[i].AsDateTime;
              Ex:=Db;
              e:=@Ex;
              p:=@db;
             Tmp[i].vExtended:=PExtended(e);
            end;

 end;
end;
Result:=Tmp; // worth a try but it didn't work

end;

when this is run I keep getting the Invalid floating point operation

If anyone has a clue, please help.
Tia,
     Paul Sutton

 

Re:Before I go Insane Need help with TVarRec and TDateTime


Remember that vExtended^ points to a memory location. Make sure this memory
location stays available (it should not defined on the local stack !).

Quote
"Paul Sutton" <phsut...@mindspring.com> wrote in message

news:3bbe1bc8.7153998@news.mindspring.com...
Quote
> D5, W98 SE
> All updates Applied.
> I have written a function which is supposed to return the record keys
> of a table. Why ? so I can specity Table.FindKey(arg)
> instead of Table.FindKey([value1, value2,...ValueN])   I am using a
> TVarRec to load up the fields values and will use the result of the
> function for the parameter in the findKey.  This works great until you
> try to load up a TDateTime field. Basically I'm trying to cast a
> Double, which is how TdateTime is defined to an extended type.  In
> debugging all seems fine. that is the Extended var is loaded. By
> observing the Result.vExtended^ the correct value is assigned.  How
> ever, upon completion of the function the value dissappears.  On other
> words, if you look ad dd:=LoadKeys(table) the value of
> dd[0].vExtended^ is gone, (some un godly small number).
> and Yes the first field of the table is a TDateTime field.  the code
> is as follows:

> Type
>    TKeyArr=Array of TVarRec;

> function LoadKeys(Tabl:TTable):TKeyArr;
> Var
>  Tmp:TKeyArr;
>  TsL:TStringList;
>  Nf, I:Integer;
>  p:^double;
>  e:^Extended;
>  Db:TDateTime;
>  Ex:Extended;

> begin
> TSl:=TStringList.Create;
> tabl.GetIndexNames(TSl);
> Nf:=NumChars(tabl.IndexDefs.Items[0].Fields,';');
> SetLength(Result,Nf+1);
> SetLength(Tmp,Nf+1);

> For I:=0 to Nf do begin

>  Case  tabl.Fields[I].DataType of
>     ftInteger:begin
>               Tmp[i].VType:=VtInteger;
>               Tmp[i].vInteger:=Tabl.Fields[i].AsInteger;
>               end;

>      ftString:begin
>               Tmp[i].Vtype:=vtAnsiString;
>               AnsiString(Tmp[i].VansiString):=Tabl.Fields[i].AsString
>               end;

>        ftDate:begin
>               Tmp[i].Vtype:=VtExtended;
>               db:= Tabl.Fields[i].AsDateTime;
>               Ex:=Db;
>               e:=@Ex;
>               p:=@db;
>              Tmp[i].vExtended:=PExtended(e);
>             end;

>  end;
> end;
> Result:=Tmp; // worth a try but it didn't work

> end;

> when this is run I keep getting the Invalid floating point operation

> If anyone has a clue, please help.
> Tia,
>      Paul Sutton

Other Threads