表示: Delphi C++
表示設定

バインド可能なオブジェクトを定義および実装する

提供:RAD Studio XE2
移動: 案内, 検索

チュートリアル:LiveBinding をプログラムで使用する への移動


注意: ここで説明しているプログラム的手法は、バインディング式を実装する標準的な方法ではありません。VCL アプリケーションや FMX アプリケーションでは、通常なら、[オブジェクト インスペクタ](標準的な手法)を使用します。バインディング式の作成にプログラム的手法を用いる必要はないでしょう。ただし、そのような式を手動で作成することも可能であり、このチュートリアルではそれを例を用いて示します。
LiveBinding を標準的な方法で使用する例については、「チュートリアル:VCL アプリケーションで LiveBinding を使用する」および「チュートリアル:LiveBinding を使用してコードを書かずに FireMonkey アプリケーションを作成する」を参照してください。

この段階では、バインド対象となる 2 つのバインド可能なオブジェクトを定義しなければなりません。たとえば、それらを TMyObject1 および TMyObject2 としましょう。これらのオブジェクトは、以下に示す 2 つのクラスとして実装されます。各クラスには 2 つのプロパティがあります。一方は String 型で、もう一方は Integer 型です。

第 1 のオブジェクトの定義

 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};
 };

第 2 のオブジェクトの定義

 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};
 };

ご覧のように、簡単にするために、両方のオブジェクトはまったく同じように宣言されています。これらのオブジェクトにはメソッドは含まれていないので、実装は不要です。コンストラクタとデストラクタは System.TObject から継承されており、オブジェクトを単にインスタンス化し自由に破棄することができます。

各オブジェクトのインスタンス化と破棄については、以下のコード断片を参照してください。

 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;
 }

次に、MyResultObject という第 3 のオブジェクトを宣言しますが、これには、先ほど定義した 2 つのオブジェクトのバインド データがすべて格納されます。

 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};
 };

一貫性を保つために、この第 3 のオブジェクトは他の 2 つのオブジェクトと似ています。

次のトピック

前のトピック

以前のバージョン
他言語版