The Entire Project and Observing the Results
Go Up to Tutorial: Using LiveBinding Programatically
- Caution: The programmatic method described here is NOT the standard way to implement binding expressions. Typically, you would use the Object Inspector (the standard method) in a graphical application. You might never need to use the programmatic way of creating binding expressions. This tutorial demonstrates, however, that it is possible to manually create such expressions.
- For examples of using LiveBindings in the standard way, see:
This topic contains the entire listing of the project and also the interpretation of the console output results.
The Project Listing
Delphi:
program LiveBindingProgr;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Bindings.Expression,
System.Bindings.ObjEval,
System.Bindings.Helper;
type
TMyObject1 = class(TObject)
private
FIntegerValue: Integer;
FStringValue: String;
public
property IntegerValue: Integer read FIntegerValue write FIntegerValue;
property StringValue: String read FStringValue write FStringValue;
end;
TMyObject2 = class(TObject)
private
FIntegerValue: Integer;
FStringValue: String;
public
property IntegerValue: Integer read FIntegerValue write FIntegerValue;
property StringValue: String read FStringValue write FStringValue;
end;
TMyResultObject = class(TObject)
private
FIntegerValue: Integer;
FStringValue: String;
public
property IntegerValue: Integer read FIntegerValue write FIntegerValue;
property StringValue: String read FStringValue write FStringValue;
end;
var
MyObject1: TMyObject1;
MyObject2: TMyObject2;
MyResultObject: TMyResultObject;
BindingExpression1: TBindingExpression;
BindingExpression2: TBindingExpression;
begin
MyObject1 := TMyObject1.Create;
MyObject2 := TMyObject2.Create;
MyResultObject := TMyResultObject.Create;
try
MyObject1.IntegerValue := 1;
MyObject1.StringValue := 'LiveBinding ';
MyObject2.IntegerValue := 2;
MyObject2.StringValue := 'power.';
{ a binding expression that binds the two Integer properties of the given objects }
BindingExpression1 := TBindings.CreateManagedBinding(
{ inputs }
[TBindings.CreateAssociationScope([
Associate(MyObject1, 'o1'),
Associate(MyObject2, 'o2')
])],
'o1.IntegerValue + o2.IntegerValue',
{ outputs }
[TBindings.CreateAssociationScope([
Associate(MyResultObject, 'res')
])],
'res.IntegerValue',
nil);
{ a binding expression that binds the two String properties of the given objects }
BindingExpression2 := TBindings.CreateManagedBinding(
{ inputs }
[TBindings.CreateAssociationScope([
Associate(MyObject1, 'o1'),
Associate(MyObject2, 'o2')
])],
'o1.StringValue + o2.StringValue',
{ outputs }
[TBindings.CreateAssociationScope([
Associate(MyResultObject, 'res')
])],
'res.StringValue',
nil);
TBindings.Notify(MyObject1, 'IntegerValue');
TBindings.Notify(MyObject1, 'StringValue');
Writeln('Result of add operation: ', MyResultObject.IntegerValue);
Writeln(MyResultObject.StringValue);
Readln;
finally
MyObject1.Free;
MyObject2.Free;
MyResultObject.Free;
end;
end.
- Tip: When you create the C++Builder Console Application, be sure to choose the Visual Component Library as Target Framework for the project to compile and link correctly.
C++:
#pragma hdrstop
#pragma argsused
#pragma explicit_rtti methods()
#include <tchar.h>
#include <stdio.h>
#include <System.Bindings.Expression.hpp>
#include <System.Bindings.Helper.hpp>
class TMyObject1 : public TObject {
private:
Integer FIntegerValue;
String FStringValue;
public:
__property Integer IntegerValue = {
read = FIntegerValue, write = FIntegerValue};
__property String StringValue = {read = FStringValue, write = FStringValue};
};
class TMyObject2 : public TObject {
private:
Integer FIntegerValue;
String FStringValue;
public:
__property Integer IntegerValue = {
read = FIntegerValue, write = FIntegerValue};
__property String StringValue = {read = FStringValue, write = FStringValue};
};
class TMyResultObject : public TObject {
private:
Integer FIntegerValue;
String FStringValue;
public:
__property Integer IntegerValue = {
read = FIntegerValue, write = FIntegerValue};
__property String StringValue = {read = FStringValue, write = FStringValue};
};
int _tmain(int argc, _TCHAR* argv[]) {
TMyObject1 *MyObject1;
TMyObject2 *MyObject2;
TMyResultObject *MyResultObject;
TBindingExpression *BindingExpression1, *BindingExpression2;
MyObject1 = new TMyObject1();
MyObject2 = new TMyObject2();
MyResultObject = new TMyResultObject();
MyObject1->IntegerValue = 1;
MyObject1->StringValue = "LiveBinding ";
MyObject2->IntegerValue = 2;
MyObject2->StringValue = "power.";
try {
try {
OpenArray<_di_IScope>InputScopes
(TBindings::CreateAssociationScope
(OPENARRAY(TBindingAssociation, (Associate(MyObject1, "o1"),
Associate(MyObject2, "o2")))));
OpenArray<_di_IScope>OutputScopes
(TBindings::CreateAssociationScope
(OPENARRAY(TBindingAssociation, (Associate(MyResultObject,
"res")))));
BindingExpression1 = TBindings::CreateManagedBinding(InputScopes,
InputScopes.GetHigh(), "o1.IntegerValue + o2.IntegerValue",
OutputScopes, OutputScopes.GetHigh(), "res.IntegerValue", NULL);
BindingExpression2 = TBindings::CreateManagedBinding(InputScopes,
InputScopes.GetHigh(), "o1.StringValue + o2.StringValue",
OutputScopes, OutputScopes.GetHigh(), "res.StringValue", NULL);
TBindings::Notify(MyObject1, "IntegerValue");
TBindings::Notify(MyObject1, "StringValue");
printf("Result of add operation: %d\n", MyResultObject->IntegerValue);
printf("%ls\n", MyResultObject->StringValue.c_str());
}
catch (Exception& e) {
printf("%ls\n", e.ToString().c_str());
}
}
__finally {
delete MyObject1;
delete MyObject2;
delete MyResultObject;
}
return 0;
}
Results interpretation
The program described in this tutorial is used to bind four properties of two different objects through binding expressions. Two of these properties are of type Integer; you performed an addition using them. The other two properties are of type string; you performed a concatenation using them.
By running the program, the console output should be as follows.
Result of add operation: 3 LiveBinding power.