Définition et implémentation des objets liables
Remonter à Tutoriel : Utilisation de LiveBinding par programme
Attention : La méthode par programme 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) dans une application VCL ou FMX. Vous ne devriez jamais avoir besoin d'utiliser la méthode de création d'expressions de liaison par programme. 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 Tutoriel : Utilisation de LiveBinding dans les applications VCL et Tutoriel : Utilisation de LiveBinding pour créer une application FireMonkey sans code.
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
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}; };
Définition du deuxième objet
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}; };
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 suppression de chaque objet :
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; }
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.
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}; };
Par souci de cohérence, ce troisième objet est similaire aux autres objets.