“No Code” Calculated Fields in RAD Studio and AppMethod

In Perth last week I was showing the new FieldOptions property on datasets. As part of the demo, I had to create a calculated field, and when I brought up the New Field dialog an audience member suggested I create an InternalCalc field instead.

I’d never heard of an InternalCalc field at the time, so later I did some research into what they are. In FireDAC they do everything a normal Calculated Field does, but in addition, you can define the value using the DefaultExpression property of the field, rather than having to write an OnCalcFields event handler.

The New Field dialog, showing the 5 different types of fields you can create

The New Field dialog, showing the 5 different types of fields you can create

In the Object Inspector screen shot below, you can see I have a TStringField with its FieldKind set to fkInternalCalc and the DefaultExpression set to:

LAST_NAME + ', ' + FIRST_NAME

Object Inspector showing the DefaultExpression and FieldKind properties of my InternalCalc field

Object Inspector showing the DefaultExpression and FieldKind properties of my InternalCalc field

This not only lets me avoid adding code for simple field calculations, but further, the calculated value is visible at design-time, unlike normal calculated fields.

The InternalCalc field at design-time

The InternalCalc field at design-time

There are more details¬†in the docs here¬†and more details on writing the expressions here. On the same page it discusses Aggregated Fields, something else I haven’t played with. Have to add it to my list.

2 Comments

  • another nice thing about internalcalc fields is that you can sort on the field and if you do
    write a oncalcfields method where you only process it when dataset state is equal to internalcalc which ends up being a lot more efficient.

  • I haven’t tried this with FireDAC yet but with TClientDataSets another useful but undocumented characteristic of internal calc fields is that their value can be set just like any other field as part of regular TDataSet.Edit/Post operations.

    Quite useful for creating temporary fields that for example tie into the UI such as a boolean IsSelected internal calc field that you link to a dataaware checkbox.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">