掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
memorystreamを文字列として変数等に入れたい (ID:152049)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
使われている用語が微妙に違うので混乱しているのかも > streamを文字列にできれば TStringStreamクラスがあるぐらいなので ストリームは文字列対応です もしかして「シリアライズ化」ではないでしょうか? > invalid stream formatとなり参照できない感じでした。 ObjectBinaryToTextは基本 TPersistentやTComponentを継承したクラスの published に定義されたプロパティを保存読み込むものです 昔書いた参考プログラムを書きますが結構古いです クラス定義 type TTestComp = class(TComponent) private { Private 宣言 } FVInt: Integer; FVStr: string; FVBool: Boolean; FVDouble: Double; FVInt64: Int64; FVSet: TTestSet; FVColor: TColor; FVFontStyle: TFontStyles; FVDate: TDateTime; FVComp: TTestCompSub; FVFont: TFont; FVValiant: Variant; FVType: TTestType; public { Public 宣言 } published property VInt : Integer read FVInt write FVInt default 1; property VStr : string read FVStr write FVStr; property VBool : Boolean read FVBool write FVBool; property VDouble : Double read FVDouble write FVDouble; property VInt64 : Int64 read FVInt64 write FVInt64 default 123456789; property VType : TTestType read FVType write FVType; property VSet : TTestSet read FVSet write FVSet; property VColor : TColor read FVColor write FVColor; property VDate : TDateTime read FVDate write FVDate; property VFont : TFont read FVFont write FVFont; property VComp : TTestCompSub read FVComp write FVComp; property VValiant : Variant read FVValiant write FVValiant; end; // フォームの宣言部に定義 private { Private 宣言 } FTestComp : TTestComp; FSaveObj : TComponent; function BinToTextOut(m : TMemoryStream) : string; // フォームの生成イベントで生成 procedure TForm1.FormCreate(Sender: TObject); begin FTestComp := TTestComp.Create(Self); end; // フォームの破棄イベントで破棄 procedure TForm1.FormDestroy(Sender: TObject); begin FTestComp.Free; end; // ObjectBinaryToTextを使いやすくする関数 引数ストリームクラス 返り値テキスト化されたデータ function TForm1.BinToTextOut(m: TMemoryStream): string; var StrStream: TStringStream; s: string; begin StrStream := TStringStream.Create(s); try m.Seek(0, soFromBeginning); ObjectBinaryToText(m, StrStream); StrStream.Seek(0, soFromBeginning); Result:= StrStream.DataString; finally StrStream.Free; end; end; // ボタンクリックイベント procedure TForm1.Button1Click(Sender: TObject); var e : TMemoryStream; s : string; begin // ここで保存/読込で試すオブジェクトを指定 フォームに色々置いて試す case of 0 : FSaveObj := Form1; 1 : FSaveObj := Form1.Button1; 2 : FSaveObj := Form1.Memo1; 3 : FSaveObj := Form1.Panel1; 4 : FSaveObj := Form1.Label1; 5 : FSaveObj := FTestComp; end; e := TMemoryStream.Create; try e.WriteComponent(FSaveObj); // ストリームにクラスの要素(実行時型情報)が送られる(バイナリ) s := BinToTextOut(e); // ストリームに書き込まれた値(実行時型情報)をテキスト化(シリアライズ) finally e.Free; end; end;
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.