Board index » delphi » Choosing Dataset['FieldName'] or Dataset.FieldByName('FieldName').AsString

Choosing Dataset['FieldName'] or Dataset.FieldByName('FieldName').AsString

Is there a benefit in using Dataset.FieldByName('FieldName').AsString [or
any type] than using Dataset['FieldName'] . I assume the second format has
to convert from a variant to whatever data type your data actually is, but
is the conversion time significant. For a function that performs lots of
assignments does the dataset['FieldName'] format slow it down?

Thanx,

Chikeh

 

Re:Choosing Dataset['FieldName'] or Dataset.FieldByName('FieldName').AsString


Hi Chikeh!

On Fri, 11 Feb 2000 13:57:23 -0500, "Chikeh" <chi...@omegalnk.com>
wrote:

Quote
>Is there a benefit in using Dataset.FieldByName('FieldName').AsString [or
>any type] than using Dataset['FieldName'] . I assume the second format has
>to convert from a variant to whatever data type your data actually is, but
>is the conversion time significant. For a function that performs lots of
>assignments does the dataset['FieldName'] format slow it down?

I would advise you to use Dataset.FieldByName('FieldName').AsString
since this will allways give you a string result while with
Dataset['FieldName'] you can have some translation errors when dealing
with nulls since the result is variant.

The best thing is to check if the field is null separately with
Dataset.FieldByName('FieldName').IsNull and then manipulate it's
value.

tomi.

Re:Choosing Dataset['FieldName'] or Dataset.FieldByName('FieldName').AsString


The Table1['FieldName'] syntax returns a variant and is, for all practical
purposes, the same as Table1.FieldByName('FieldName').Value. Considert the
following.

var
  I:  Integer
begin
  I := Table1['SomeIntegerField'];

If the field in the table is null you will get an "invalid variant type
conversion" error because I cannot store the value null. If you use I :=
FieldByName('SomeIntegerField').AsInteger and the field is null I will be
set to zero and no error will occur. The m{*word*203}to this story is that if you
assign a field value as a variant you had better assign it to a variant
variable because with any other type of variable you will get "invalid
variant type conversion" when you encounter a null field.

Bill

Bill Todd (TeamB)
(TeamB cannot respond to questions received via email)

Other Threads