关于TobjectList的一点疑问 VCL组件开发及应用http://www.delphi2007.net/DelphiVCL/html/delphi_20061223112301168.html
我用了一个TObjectList,我想将自己定义的类对象加进去(包含多个对象)
对象含有多个字段(属性),最后我要将各个对象,以某字段的值进行排序
下面是程序代码片段----------
CoDeskList := TObjectList.Create;
CoDesKList.OwnsObjects := false;//这个地方采用默认值还是false
while not eof do//数据库读出的记录
begin
SvDesk := TSvDesk.Create;//自定义对象
SvDesk.DCoID:= Trim(fieldbyname('servdeskcoid').AsString);
SvDesk.Length := fieldbyname('inorderqueuecurrlength').AsInteger;
SvDesk.Pri := fieldbyname('pri').AsInteger;
CoDeskList.Add(SvDesk);
SvDesk.Free;//释放
next;
end;
MaxPri := TSvDesk(CoDeskList.Items[0]).Pri;//TSvDesk强制转换 调试过,有值
ID:= TSvDesk(CoDeskList.Items[0]).DcoID;//这里为什么读不出来呢
For i:= 0 TO Sum_a Do //找出服务台的最高优先级值
begin
if MaxPri < TSvDesk(CoDeskList.Items[i]).Pri then
MaxPri := TSvDesk(CoDeskList.Items[i]).Pri;
end;
最后
CoDeskList.Clear;
CoDeskList.Free;
还有一个很严重的问题就是CoDeskList里有两条记录,但都是最后一条记录的值(第一条被覆盖掉了),为什么呢?????????
type
TSvDesk = class
private
FDCoID: string;
FLength: Integer;
FPri: Integer;
protected
public
property DCoID: string read FDCoID write FDCoID;
property Length: Integer read FLength write FLength;
property Pri: Integer read FPri write FPri;
end;
TFormDemo = class(TForm)
ADOQuery: TADOQuery;
ButtonDemo: TButton;
procedure ButtonDemoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormDemo: TFormDemo;
implementation
{$R *.dfm}
//排序函数(降序)
function ComparePri(Item1, Item2: TObject): Integer;
begin
if TSvDesk(Item1).Pri > TSvDesk(Item2).Pri then
Result := -1;
if TSvDesk(Item1).Pri = TSvDesk(Item2).Pri then
Result := 0;
if TSvDesk(Item1).Pri < TSvDesk(Item2).Pri then
Result := 1;
end;
procedure TFormDemo.ButtonDemoClick(Sender: TObject);
var
CoDeskList: TObjectList;
SvDesk: TSvDesk;
MaxPri: Integer;
ID: Integer;
begin
CoDeskList := TObjectList.Create;
//CoDesKList.OwnsObjects := False; //Default = True
try
with ADOQuery do
begin
while not Eof do//数据库读出的记录
begin
SvDesk := TSvDesk.Create;//自定义对象
SvDesk.DCoID:= Trim(FieldByName('ServDeskCoID').AsString);
SvDesk.Length := FieldByName('InOrderQueueCurrLength').AsInteger;
SvDesk.Pri := FieldByName('Pri').AsInteger;
CoDeskList.Add(SvDesk);
//SvDesk.Free;//不要释放
Next;
end;
end;
CoDeskList.Sort(@ComparePri);
MaxPri := TSvDesk(CoDeskList.Items[0]).Pri;
finally
CoDeskList.Free;
end;
end;
DELPHI HELP
OwnsObjects:
Allows TObjectList to free objects when they are deleted from the list or the list is destroyed.
OwnsObjects allows TObjectList to control the memory of its objects. If OwnsObjects is true (the default),
calling Delete or Remove frees the deleted object in addition to removing it from the list.
calling Clear frees all the objects in the list in addition to emptying the list.
calling the destructor frees all the objects in the list in addition to destroying the TObjectList itself.
assigning a new value to an index in Items frees the object that previously occupied that position in the list.
还有一个很严重的问题就是CoDeskList里有两条记录,但都是最后一条记录的值(第一条被覆盖掉了),为什么呢?????????
------------------------------------
呵呵。 一切都是指针惹的祸。