掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
TListでの二次元配列クラスの作成 (ID:9074)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
>もしよろしければ、jokさんの方法をもう少し具体的に教えていただけませんか? こんな感じです。ポインタの二次元配列を操作するクラスを TwoDArrayUnit.pas につくりました。 -------------------------------------------- unit TwoDArrayUnit; interface uses SysUtils, Classes, Dialogs; type TPointerGrid = class(TObject) private FPArray: array of array of pointer; FColCount: integer; FRowCount: integer; procedure SetColCount(const Value: integer); procedure SetRowCount(const Value: integer); function GetItems(iCol,iRow:integer): pointer; procedure SetItems(iCol,iRow:integer; const Value: pointer); protected public constructor Create(InitColCount,InitRowCount:integer); destructor Destroy;override; property ColCount:integer read FColCount write SetColCount; property RowCount:integer read FRowCount write SetRowCount; property Items[iCol,iRow:integer]:pointer read GetItems write SetItems; end; implementation { TPointerGrid } constructor TPointerGrid.Create(InitColCount, InitRowCount: integer); begin FColCount := InitColCount; FRowCount := InitRowCount; SetLength(FPArray,FColCount,FRowCount); end; destructor TPointerGrid.Destroy; begin inherited; end; procedure TPointerGrid.SetColCount(const Value: integer); begin FColCount := Value; SetLength(FPArray,FColCount,FRowCount); end; procedure TPointerGrid.SetRowCount(const Value: integer); begin FRowCount := Value; SetLength(FPArray,FColCount,FRowCount); end; function TPointerGrid.GetItems(iCol,iRow:integer): pointer; begin result := nil; if (iCol>=FColCount) or (iRow>=FRowCount) then ShowMessage('範囲外のアクセスです') else result := FPArray[iCol,iRow]; end; procedure TPointerGrid.SetItems(iCol,iRow:integer; const Value: pointer); begin if (iCol>=FColCount) or (iRow>=FRowCount) then ShowMessage('範囲外のアクセスです') else FPArray[iCol,iRow] := Value; end; end. -------------------------------------------- まだ基本的な部分だけです。行・列の途中に行・列を挿入したり、削除したりの メソッドは未実装です。property Cols:TList なども未実装ですが、感じがつかめ るでしょう。これをテストするコードは、Button1 とStringGrid1をつかって -------------------------------------------- unit Main; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, TwoDArrayUnit, StdCtrls, Grids; type TForm1 = class(TForm) StringGrid1: TStringGrid; Button1: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private 宣言 } public PointerGrid:TPointerGrid; end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var iCol,iRow:integer; begin PointerGrid := TPointerGrid.Create(5,5); for iRow := 0 to PointerGrid.RowCount-1 do for iCol := 0 to PointerGrid.ColCount-1 do PointerGrid.Items[iCol,iRow] := pointer(iCol*iRow); end; procedure TForm1.FormDestroy(Sender: TObject); begin PointerGrid.Free; end; procedure TForm1.Button1Click(Sender: TObject); var iCol,iRow:integer; begin for iRow := 0 to PointerGrid.RowCount-1 do for iCol := 0 to PointerGrid.ColCount-1 do StringGrid1.Cells[iCol,iRow] := IntToStr(integer(PointerGrid.Items[iCol,iRow])); end; end. -------------------------------------------- うまくいっているようです。特定のレコードのポインタの配列の場合は、入出力の型を pointer から PRec などに変えてください。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.