Board index » delphi » 2 ways to refer field in Data Module

2 ways to refer field in Data Module

Hello

To refer a field in table, I can use

DataModule1.Table1.FieldByName(fieldname).AsString

or

DataModule1.Table1FieldName1.Value

Which way is better?

Thanks

pc

 

Re:2 ways to refer field in Data Module


hi pc

Use

Quote
> DataModule1.Table1.FieldByName(fieldname).AsString

when you don't put fields in a table (with field explorer)
and use
Quote
> DataModule1.Table1.FieldByName(fieldname).AsString

when you add those fields.
Difference is that the first one isn't hardcoded in your app, so you won't
get a compiler error doing FieldByName('wrongFieldName'), but you'll get a
runtime error, second is an object of TField (actually of TFloatField,
TIntegerField...) and is hardcoded in your app.
You usualy use first aproach when building apps like SQL Explorer (you
don't know which table will be opened, so you cannot create fields at
design time).
About which one is better... I'm not quite sure so somebody might correct
me, but... since second is a hardcoded object and first one is a function
that finds your field in some kind of an array, I'd say first aproach is a
bit faster.

Hope it helps

Lucijan

Your Name <dle...@ouhk.edu.hk> wrote in article
<3785A89A.ADA15...@ouhk.edu.hk>...

Quote
> Hello

> To refer a field in table, I can use

> DataModule1.Table1.FieldByName(fieldname).AsString

> or

> DataModule1.Table1FieldName1.Value

> Which way is better?

> Thanks

> pc

Other Threads