Show: Delphi C++
Display Preferences

Defining and Implementing the Bindable Objects

From RAD Studio XE3
Jump to: navigation, search

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 VCL or an FMX 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 Tutorial: Using LiveBinding in VCL Applications and Tutorial: Using LiveBinding to Create a FireMonkey Application Without Code.

At this point you should define two bindable objects that you want to bind together. For example, call them TMyObject1 and TMyObject2. These objects are implemented as two classes, as shown below. Each one has two properties—one is a String and the other one is an Integer.

Definition of the first object

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;
class TMyObject1 : public TObject {
private:
	Integer FIntegerValue;
	String FStringValue;
 
public:
	__property Integer IntegerValue = {
		read = FIntegerValue, write = FIntegerValue};
	__property String StringValue = {read = FStringValue, write = FStringValue};
};

Definition of the second object

type
  TMyObject2 = class(TObject)
  private
    FIntegerValue: Integer;
    FStringValue: String;
  public
    property IntegerValue: Integer read FIntegerValue write FIntegerValue;
    property StringValue: String read FStringValue write FStringValue;
  end;
class TMyObject2 : public TObject {
private:
	Integer FIntegerValue;
	String FStringValue;
 
public:
	__property Integer IntegerValue = {
		read = FIntegerValue, write = FIntegerValue};
	__property String StringValue = {read = FStringValue, write = FStringValue};
};

You can see that, for simplicity, both objects are declared in exactly the same way. As these objects do not contain any methods, no implementation is required. The constructor and destructor are inherited from System.TObject and you can simply instantiate them and freely destroy them.

Instantiating and destroying each object:

var
  MyObject1: TMyObject1;
  MyObject2: TMyObject2;
 
begin
  MyObject1 := TMyObject1.Create;
  MyObject2 := TMyObject2.Create;
  try
    { various binding operations - see next topic / step }
  finally
    MyObject1.Free;
    MyObject2.Free;
  end;
end;
int _tmain(int argc, _TCHAR* argv[]) {
	TMyObject1 *MyObject1 = new TMyObject1();
	TMyObject2 *MyObject2 = new TMyObject2();
 
	try
	{
		// various binding operations - see next topic / step
	}
	__finally {
		delete MyObject1;
		delete MyObject2;
		delete MyResultObject;
	}
 
	return 0;
}

A third object called MyResultObject is to be declared and will hold all bound data of the other two objects previously defined.

type
  TMyResultObject = class(TObject)
  private
    FIntegerValue: Integer;
    FStringValue: String;
  public
    property IntegerValue: Integer read FIntegerValue write FIntegerValue;
    property StringValue: String read FStringValue write FStringValue;
  end;
class TMyResultObject : public TObject {
private:
	Integer FIntegerValue;
	String FStringValue;
 
public:
	__property Integer IntegerValue = {
		read = FIntegerValue, write = FIntegerValue};
	__property String StringValue = {read = FStringValue, write = FStringValue};
};

For consistency, this third object is similar to the other ones.

Next

Previous

Personal tools
Previous Versions
In other languages