LiveBindings Inc Method (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to create a LiveBindings method, Inc, that increases an integer by 1.

To build and test this example:

  1. Create a package and add a unit to it.
  2. Add the following units to the uses subsection of the implementation section:
  3. Define a function that returns an instance of IInvokable. This instance is a function that expects an integer and returns this integer increased by one. For example, if the function receives 5, it returns 6.
  4. In the initialization section, register your invokable function with 'Inc' as both name and ID.
  5. In the finalization section, unregister your invokable function.
Note: After installing the package with custom LiveBindings Methods, re-open the form or project using TBindingsList component.
Note: When application is build with run-time packages, you need to load the package with custom LiveBindings Methods using LoadPackage call. This must be done before the creation of forms which use these methods.

You can now install your package. Your method is registered and you can use it in your custom format and parse expressions. Your method is also listed in the LiveBindings Methods editor.

LiveBindingsIncFormattingMethodBefore.png
LiveBindingsIncFormattingMethodAfter.png

Code

unit IncMethod;

interface

implementation

uses System.Bindings.Methods, System.Bindings.EvalProtocol, System.Bindings.Consts, System.TypInfo;

function MakeMethodInc: IInvokable;
begin
  Result := MakeInvokable(function(Args: TArray<IValue>): IValue
  var
    InputValue: IValue;
    InputInteger, OutputInteger: Int64;
  begin
    // Ensure only one argument is received.
    if Length(Args) <> 1 then
      raise EEvaluatorError.Create(sFormatArgError);

    // Ensure that the received argument is an integer.
    InputValue := Args[0];
    if not (InputValue.GetType.Kind in [tkInteger, tkInt64]) then
      raise EEvaluatorError.Create(sFormatExpectedStr);

    // Calculate the output integer.
    InputInteger := InputValue.GetValue.AsInt64;
    OutputInteger := InputInteger + 1;

    // Return the output integer.
    Result := TValueWrapper.Create(OutputInteger);
  end);
end;

initialization

  TBindingMethodsFactory.RegisterMethod(
    TMethodDescription.Create(
      MakeMethodInc,
      'Inc',
      'Inc', '', True,
      sFormatDesc,
      nil));

finalization

  TBindingMethodsFactory.UnRegisterMethod('Inc');

end.

Uses

See Also