Définition et implémentation des objets liables

De RAD Studio
Aller à : navigation, rechercher

Remonter à Tutoriel : Utilisation de LiveBindings par programmation


Attention : La méthode par programmation décrite ici n'est PAS le moyen standard d'implémenter les expressions de liaison. Typiquement, vous devriez utiliser l'inspecteur d'objets (la méthode standard) à la conception. Vous ne devriez jamais avoir besoin d'utiliser la méthode de création d'expressions de liaison par programmation. Toutefois, ce tutoriel montre qu'il est possible de créer manuellement de telles expressions.


Pour obtenir des exemples d'utilisation de LiveBindings selon la méthode standard, voir :

A ce stade, vous devez définir deux objets liables que vous voulez lier ensemble. Par exemple, appelez-les TMyObject1 et TMyObject2. Ces objets sont implémentés comme deux classes, comme illustré ci-dessous. Chaque objet a deux propriétés — l'une est un String et l'autre est un Integer.

Définition du premier objet

Delphi :
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;
C++ :
class TMyObject1 : public TObject {
private:
	Integer FIntegerValue;
	String FStringValue;

public:
	__property Integer IntegerValue = {
		read = FIntegerValue, write = FIntegerValue};
	__property String StringValue = {read = FStringValue, write = FStringValue};
};

Définition du deuxième objet

Delphi :
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;
C++ :
class TMyObject2 : public TObject {
private:
	Integer FIntegerValue;
	String FStringValue;

public:
	__property Integer IntegerValue = {
		read = FIntegerValue, write = FIntegerValue};
	__property String StringValue = {read = FStringValue, write = FStringValue};
};

Vous pouvez voir que, par souci de simplicité, les deux objets sont déclarés exactement de la même façon. Comme ces objets ne contiennent pas de méthodes, aucune implémentation n'est requise. Le constructeur et le destructeur sont hérités de System.TObject et vous pouvez simplement les instancier et les détruire librement.

Instanciation et destruction de chaque objet :

Delphi :
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;
C++ :
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;
}

Un troisième objet appelé MyResultObject est à déclarer et il contiendra toutes les données liées des deux autres objets préalablement définis.

Delphi :
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;
C++ :
class TMyResultObject : public TObject {
private:
	Integer FIntegerValue;
	String FStringValue;

public:
	__property Integer IntegerValue = {
		read = FIntegerValue, write = FIntegerValue};
	__property String StringValue = {read = FStringValue, write = FStringValue};
};

Par souci de cohérence, ce troisième objet est similaire aux autres objets.

Suivant

Précédent