我用了一个TObjectList,我想将自己定义的类对象加进去(包含多个对象)
对象含有多个字段(属性),最后我要将各个对象,以某字段的值进行排序下面是程序代码片段----------
CoDeskList := TObjectList.Create;
CoDesKList.OwnsObjects := false;//这个地方采用默认值还是falsewhile 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里有两条记录,但都是最后一条记录的值(第一条被覆盖掉了),为什么呢?????????
对象含有多个字段(属性),最后我要将各个对象,以某字段的值进行排序下面是程序代码片段----------
CoDeskList := TObjectList.Create;
CoDesKList.OwnsObjects := false;//这个地方采用默认值还是falsewhile 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里有两条记录,但都是最后一条记录的值(第一条被覆盖掉了),为什么呢?????????
解决方案 »
- 请教一个记录方面的问题(问题简单,请各位赐教)
- 如何获取Image控件内图片的地址
- update 更新数据后,如何返回知道更新的记录数
- 安装了一个tms组件,一切正常,但在调用属性或方法的时候,显示不出来,极少机会可以,请教各位!
- Delphi+sql server编写的程序 但是自己不在客户的机子上安装sql server(在线等待 解决马上揭帖)
- 在DELPHI 和 MS SQL SERVER中建库和建表的问题
- demos/midas/pooler下的例程,那位大虾给解释一下,重分相酬!
- firetoucher的善分帖一
- 线程的循环使用过程!急急急!!!!
- 捡分问题,快来看看。
- 窗体form失去焦点时(非激活状态),发生什么事件
- 用WNetAddConnection2建立映射后,如何隐藏映射盘符?
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;
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.
------------------------------------
呵呵。 一切都是指针惹的祸。