8
When I have a ClientDataset
, one MemoryTable
or a Query
, what’s the difference if I take the value of a field using FieldByName()
or the variable associated with the field?
In the example below, I took the last ID of a field in the bank and differentiated between the two ways to get the value of the query:
fDm.fdqHistorico.Open;
iId := 0;
if fDm.fdqHistorico.RecordCount > 0 then
begin
iId := fDm.fdqHistorico.FieldByName('his_id').AsInteger; // Modo 1
iId := fDm.fdqHistoricohis_id.AsInteger; // Modo 2
end;
Inc(iId);
- Mode 1: Fieldbyname
- Mode 2: Associated variable
Is there any difference in performance or anything else? Or is it just the same?
An additional to @bigown’s reply: Fieldbyname('his_id') will only present error when the application already 'runs' the error association in the development (if any)!
– Junior Moreira
Complementing, many people say that using associated variable does not work
debug
(not to see the value), for trigger, I tested now and works the same way. ;)– David
Exact, both in CTRL+F7 and passing the mouse over the type (Asinteger, Asfloat, Asstring) etc...
– Junior Moreira
Mode 2 is also faster, plus all the advantages quoted by @bigown, because you don’t need to search the query/dataset field list.
– Carlos Bridi